2013-04-16 123 views

回答

71

沒錯,關鍵是一個值應用於NSStrokeWidthAttributeName 如果這個值是正數,你將只能看到行程,而不是填充。

目的-C:

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

由於下面@cacau:參見Technical Q&A QA1531

夫特版本:

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

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

夫特3版本:

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

請注意,「輪廓」將是一個內部輪廓,而不是外部輪廓 - 即輪廓將進入填充區域。 – Jason

+2

@Jason有沒有辦法使用UILabel + AttributedText生成一個「外部」填充? –

+0

我還沒有找到如何... – Jason

7

斯威夫特版本:

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

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

斯威夫特3版本:

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

現在迅速蘋果最新 刪除[字符串:任何]的屬性鍵值聲明用作[NSAttributedStringKey:任何]

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) 

謝謝