2013-04-16 24 views

Trả lời

71

Vâng, chính là để áp dụng một Negative giá trị đến NSStrokeWidthAttributeName Nếu giá trị này là tích cực, bạn sẽ chỉ nhìn thấy đột quỵ và không điền.

Objective-C:

self.label.attributedText=[[NSAttributedString alloc] 
initWithString:@"string to both stroke and fill" 
attributes:@{ 
      NSStrokeWidthAttributeName: @-3.0, 
      NSStrokeColorAttributeName:[UIColor yellowColor], 
      NSForegroundColorAttributeName:[UIColor redColor] 
      } 
]; 

Nhờ @cacau dưới đây: Xem thêm Technical Q&A QA1531

Swift phiên bản:

let attributes = [NSStrokeWidthAttributeName: -3.0, 
         NSStrokeColorAttributeName: UIColor.yellowColor(), 
         NSForegroundColorAttributeName: UIColor.redColor()]; 

label.attributedText = NSAttributedString(string: "string to both stroke and fill", attributes: attributes) 

Swift 3 phiên bản:

let attributes = [NSStrokeWidthAttributeName: -3.0, 
         NSStrokeColorAttributeName: UIColor.yellow, 
         NSForegroundColorAttributeName: UIColor.red] as [String : Any] 

    label.attributedText = NSAttributedString(string: "string to both stroke and fill", attributes: attributes) 
+0

Lưu ý rằng "đường viền" sẽ là đường viền bên trong, không phải đường viền bên ngoài - nghĩa là đường viền sẽ ăn vào khu vực tô đầy. – Jason

+2

@ Jason có cách nào để tạo ra một điền "bên ngoài" với UILabel + AttributedText? –

+0

Tôi chưa tìm ra cách ... – Jason

7

Swift phiên bản:

let attributes = [NSStrokeWidthAttributeName: -3.0, 
         NSStrokeColorAttributeName: UIColor.yellowColor(), 
         NSForegroundColorAttributeName: UIColor.redColor()]; 

label.attributedText = NSAttributedString(string: "string to both stroke and fill", attributes: attributes) 

Swift 3 phiên bản:

let attributes = [NSStrokeWidthAttributeName: -3.0, 
         NSStrokeColorAttributeName: UIColor.yellow, 
         NSForegroundColorAttributeName: UIColor.red] as [String : Any] 

    label.attributedText = NSAttributedString(string: "string to both stroke and fill", attributes: attributes) 
0

Bây giờ mới nhất trong táo nhanh chóng loại bỏ các [String: Bất kỳ] cho các thuộc tính quan trọng sử dụng tờ khai trị giá như [NSAttributedStringKey: Bất kỳ]

let strokeTextAttributes = [ 
     NSAttributedStringKey.strokeColor : UIColor.green, 
     NSAttributedStringKey.foregroundColor : UIColor.lightGray, 
     NSAttributedStringKey.strokeWidth : -4.0, 
     NSAttributedStringKey.font : UIFont.boldSystemFont(ofSize: 52) 
     ] as [NSAttributedStringKey : Any] 

    hello_cell_lb.attributedText = NSAttributedString(string: "\(hello_array[indexPath.row])", attributes: strokeTextAttributes) 

cảm ơn bạn