在UILabel子类中概述UILabel文本

时间:2016-11-13 15:19:28

标签: ios swift uilabel stroke outline

我正在努力寻找一种方法来简单地为我的UILabel文本添加轮廓/笔触/轮廓。谈论围绕UILabel背景的文字字母的笔划。

我使用的是swift 3,我想直接将我的文字概述到我的子类中:UILabel。

我找到了多个答案,建议采用这种方式做事:

plasser@pisbak:$ cc -Wall -pg omg2.c -lm
omg2.c:8:0: warning: ignoring #pragma include  [-Wunknown-pragmas]
 #pragma include "cachelab.h"
 ^
plasser@pisbak:$ ./a.out
 c_cache.cache_set = (nil)
OMG3!
plasser@pisbak:$ 

但问题是它不起作用。我的文字仍然相同,没有大纲......

有人可以帮我吗? 那将是一件好事:)

非常感谢。干杯。

6 个答案:

答案 0 :(得分:10)

此代码适用于我。

Swift 3

let strokeTextAttributes = [
  NSStrokeColorAttributeName : UIColor.black,
  NSForegroundColorAttributeName : UIColor.white,
  NSStrokeWidthAttributeName : -4.0,
  NSFontAttributeName : UIFont.boldSystemFont(ofSize: 30)
] as [String : Any]

myLabel.attributedText = NSMutableAttributedString(string: "Test me i have color.", attributes: strokeTextAttributes)

Output like this...

Swift 4.2

let strokeTextAttributes = [
  NSAttributedString.Key.strokeColor : UIColor.red,
  NSAttributedString.Key.foregroundColor : UIColor.white,
  NSAttributedString.Key.strokeWidth : -4.0,
  NSAttributedString.Key.font : UIFont.boldSystemFont(ofSize: 30)]
  as [NSAttributedString.Key : Any]

labelOutLine.attributedText = NSMutableAttributedString(string: "Your outline text", attributes: strokeTextAttributes)

enter image description here

答案 1 :(得分:8)

@anandnimje回答转换为Swift 4.2并将其包装成函数:

public func stroke(font: UIFont, strokeWidth: Float, insideColor: UIColor, strokeColor: UIColor) -> [NSAttributedStringKey: Any]{
    return [
        NSAttributedStringKey.strokeColor : strokeColor,
        NSAttributedStringKey.foregroundColor : insideColor,
        NSAttributedStringKey.strokeWidth : -strokeWidth,
        NSAttributedStringKey.font : font
        ]
}

用法:

label.attributedText = NSMutableAttributedString(string: "Hello World", 
attributes: stroke(font: UIFont(name: "SourceSansPro-Black", size: 20)!, 
strokeWidth: 4, insideColor: .white, strokeColor: .black))

确保您的UIFont名称正确,否则崩溃。如果你有正确的名字,应该永远不会有问题。

答案 2 :(得分:6)

在这里,您可以使用实现,复制和粘贴到playgrond进行测试的课程:

    class StrokedLabel: UILabel {

        var strockedText: String = "" {
            willSet(newValue) {
                let strokeTextAttributes = [
                    NSStrokeColorAttributeName : UIColor.black,
                    NSForegroundColorAttributeName : UIColor.white,
                    NSStrokeWidthAttributeName : -4.0,
                    NSFontAttributeName : UIFont.boldSystemFont(ofSize: 30)
                    ] as [String : Any]

                let customizedText = NSMutableAttributedString(string: newValue,
                                                               attributes: strokeTextAttributes)


                attributedText = customizedText
            }
        }
    }


//////////// PLAYGROUND IMPLEMENTATION PART /////////
    let text = "Stroked text"

// UILabel subclass initialization
    let label = StrokedLabel(frame: CGRect(x: 0, y: 0, width: 200, height: 50))
// simple assign String to 'strockedText' property to see the results
    label.strockedText = text

    label.backgroundColor = UIColor.white


    label

Swift 4.2

import UIKit

class StrokedLabel: UILabel {

var strockedText: String = "" {
    willSet(newValue) {
        let strokeTextAttributes : [NSAttributedString.Key : Any] = [
            NSAttributedString.Key.strokeColor : UIColor.black,
            NSAttributedString.Key.foregroundColor : UIColor.white,
            NSAttributedString.Key.strokeWidth : -4.0,
            NSAttributedString.Key.font : UIFont.boldSystemFont(ofSize: 30)
            ] as [NSAttributedString.Key  : Any]

        let customizedText = NSMutableAttributedString(string: newValue,
                                                       attributes: strokeTextAttributes)


        attributedText = customizedText
    }
}

}

//////////// PLAYGROUND IMPLEMENTATION PART /////////
  let text = "Stroked text"

  // UILabel subclass initialization
  let label = StrokedLabel(frame: CGRect(x: 0, y: 0, width: 200, height: 50))
  // simple assign String to 'strockedText' property to see the results
  label.strockedText = text

  label.backgroundColor = UIColor.clear


  label

也许欢迎对这个课进行重构,但是应该以这种形式为你工作

enter image description here

如您所见,使用非常方便。

答案 3 :(得分:0)

如果我设置属性字典的类型,您的代码适用于我,如下所示:

let strokeTextAttributes: [String: Any] = [
  // etc...
]

也许这就是你所有的遗失?

答案 4 :(得分:0)

下面是我用Swift 4.1编写的应用程序中使用的

Swift 4.x

@ViewChild('modelTextarea') modelTextarea;
model: string;

addText(text) {
 this.model = this.model + text;
 this.modelTextarea.setFocus();
}

答案 5 :(得分:0)

  

更新为Swift 5

此答案建立在 Anandnimje J.Doe 答案的基础上,旨在更新和简化它,以使用法更加清晰和简单。

只需使用以下两个功能:

func outline(string:String, font:String, size:CGFloat, outlineSize:Float, textColor:UIColor, outlineColor:UIColor) -> NSMutableAttributedString {
    return NSMutableAttributedString(string:string,
                                     attributes: outlineAttributes(font: UIFont(name: font, size: size)!,
                                                        outlineSize: outlineSize, textColor: textColor, outlineColor: outlineColor))
}

func outlineAttributes(font: UIFont, outlineSize: Float, textColor: UIColor, outlineColor: UIColor) -> [NSAttributedString.Key: Any]{
    return [
        NSAttributedString.Key.strokeColor : outlineColor,
        NSAttributedString.Key.foregroundColor : textColor,
        NSAttributedString.Key.strokeWidth : -outlineSize,
        NSAttributedString.Key.font : font
    ]
}

然后使用带有以下标签的轮廓:

label.attributedText = outline(string: "Label Text", font: "HelveticaNeue", size: 14, outlineSize: 4, textColor: .white, outlineColor: .black)