如何使用NSAttributedString w / UILabel来描边和填充

时间:2013-04-16 22:01:23

标签: ios cocoa uilabel nsattributedstring

是否可以将描边填充同时应用于NSAttributedStringUILabel

5 个答案:

答案 0 :(得分:81)

是的,关键是将否定值应用于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

Swift版本:

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 版本:

    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)

Swift 4 版本:

let attributes: [NSAttributedStringKey : Any] = [.strokeWidth: -3.0,
                                                 .strokeColor: UIColor.yellow,
                                                 .foregroundColor: UIColor.red]

label.attributedText = NSAttributedString(string: text, attributes: attributes)

答案 1 :(得分:8)

Swift版本:

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 版本:

    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)

答案 2 :(得分:1)

Swift 4 版本:

const nameOfIV = Sample.IV.name; // "IV"
console.log(nameOfIV); // "IV"
const numberOfIII = Sample.III.number; // SampleEnum.III
console.log(numberOfIII); // 1
const descriptionOfV = Sample.V.description; // string
console.log(descriptionOfV); // "Blah V"

const goodSample: Sample = Sample.III; // okay
const badSample: Sample = {
  name: "II", 
  description: "oops", 
  number: 3
}; // error, name doesn't match

答案 3 :(得分:0)

现在最新 在swift apple中删除[String:Any]属性键值声明用作[NSAttributedStringKey:Any]

    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)

谢谢

答案 4 :(得分:0)

这是 UITextView 中的 Stroke 和 Oblique/tilt/slant 文本的示例

    public func setAttributedText(fontStyle: INSTextFontPickerStyle, strokeColor: UIColor = UIColor.white, foregroundColor: UIColor = UIColor.black, isObliqueness: Bool = false, isStrokes: Bool = false) {
        self.fontStyle = fontStyle

        let paragraphStyle = NSMutableParagraphStyle()
        paragraphStyle.alignment = innterTextView.textAlignment

        // Tilting the text
        let obliquenessValue = isObliqueness ? NSNumber(value: 0.2) : NSNumber(value: 0) // The angle of tilting in clockwise

        // Stroke a line in text edges
        var strokeWidth = NSNumber(value: 0.0)
        var _strokeColor = UIColor.clear
        if isStrokes || fontStyle == .strokeEdges {
            strokeWidth = NSNumber(value: -2.0) // The width of stroke
            _strokeColor = strokeColor
        }
        
        textView.attributedText = NSAttributedString(string: textView.text, attributes: [
            NSAttributedString.Key.foregroundColor : foregroundColor,
            NSAttributedString.Key.font : UIFont.systemFont(ofSize: (actualFontSize > 0 ? actualFontSize : fontSize)),
            NSAttributedString.Key.paragraphStyle : paragraphStyle,
            NSAttributedString.Key.strokeColor : _strokeColor,
            NSAttributedString.Key.strokeWidth : strokeWidth,
            NSAttributedString.Key.obliqueness: obliquenessValue
        ])
    }