IOS 給一個文字框,按鈕,view加虛線邊框,ios虛線

來源:互聯網
上載者:User

IOS 給一個文字框,按鈕,view加虛線邊框,ios虛線

怎麼給IOS視圖控制項加上一層虛線邊框  還是可以根據控制項大小來定呢?

應用情境: 一個文本textField跟要據文字大小來改變自己的大小。  邊框為虛線邊框。

     這時候大家可能考慮到的是 做一個虛線背景 然後展開後做為textField的邊框。

     想法很好,但出現的總是是, 在展開的那個點,正好是白色 或 者黑色的時候  就是有總是的。  白色展開後一長條全是白的 黑色也如此

 

這時候在考慮的就是 能不能即時畫一個虛線背景圖 即時設定textField的背景呢。

答案是可以的。 

我也在網上找了一些相關的資料 發現零零散散的也這方面的東西 但沒有人貼出一個完整可用的解決方案

因此 我寫了一個UIImage的分類。  可以直接調用一下 就可以直接使用了。

代碼:

+ (UIImage*)imageWithSize:(CGSize)size borderColor:(UIColor *)color borderWidth:(CGFloat)borderWidth{    UIGraphicsBeginImageContextWithOptions(size, NO, 0.0);       [[UIColor clearColor] set];    CGContextRef context = UIGraphicsGetCurrentContext();    CGContextBeginPath(context);    CGContextSetLineWidth(context, borderWidth);    CGContextSetStrokeColorWithColor(context, color.CGColor);    CGFloat lengths[] = { 3, 1 };    CGContextSetLineDash(context, 0, lengths, 1);    CGContextMoveToPoint(context, 0.0, 0.0);    CGContextAddLineToPoint(context, size.width, 0.0);    CGContextAddLineToPoint(context, size.width, size.height);    CGContextAddLineToPoint(context, 0, size.height);    CGContextAddLineToPoint(context, 0.0, 0.0);    CGContextStrokePath(context);    UIImage* image = UIGraphicsGetImageFromCurrentImageContext();    UIGraphicsEndImageContext();    return image;}

這是方法的實現。 第一個參數為 需要虛線邊框視圖的大小, 第二個為 邊框顏色, 第三個為 邊框粗細。

建立為UIImage的分類的話 直接可以用UIImage 來調用 非常方便。

  原創文章,轉載請註明出處! 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.