1

Based on the answers here I made this code:

extension NSMutableAttributedString {
    func bold(text:String, size:CGFloat) -> NSMutableAttributedString {
        let attrs:[String:AnyObject] = [NSFontAttributeName : UIFont.boldSystemFontOfSize(size)]
        let boldString = NSMutableAttributedString(string:"\(text)", attributes:attrs)
        self.appendAttributedString(boldString)
        return self
    }

    func normal(text:String)->NSMutableAttributedString {
        let normal =  NSAttributedString(string: text)
        self.appendAttributedString(normal)
        return self
    }
}

and I use it like this:

@IBOutlet weak var m_field: UITextField!
 override func viewDidLoad() {
    super.viewDidLoad()
    let string = NSMutableAttributedString()
    string.bold("Bold_text: ",size: 12).normal("normal text")
    m_field.attributedText = string
}

but it doesn't work, all my text is the same (bold I think) what am I doing wrong?

Community
  • 1
  • 1
Marinos K
  • 1,779
  • 16
  • 39
  • 1
    what is writer_string ? Probably you should do it like m_field.attributedText = string – Oleg Gordiichuk Apr 10 '17 at 09:55
  • You need the methods `setAttributes(_ attrs:range:` or `addAttribute(_ name:value:range:)` of `NSMutableAttributedString` to change attributes at a specific range. – vadian Apr 10 '17 at 09:55
  • @OlegGordiichuk that was a typo - sorry – Marinos K Apr 10 '17 at 10:06
  • @vadian can you redirect me to some example? I tried that and failed again – Marinos K Apr 10 '17 at 10:14
  • I've imported your extension in to a new project and am setting the text on a `UILabel` using it, and all works fine. Are you sure you're not overriding the text elsewhere? Or setting the `attributedText` on a different `UITextField`? – Matthew Hallatt Apr 10 '17 at 10:16
  • For example make the first two characters bold (`string` is `NSMutableAttributedString` containing at least 2 characters): `string.addAttribute(NSFontAttributeName, value: NSFont.systemFont(ofSize: 14.0), range: NSMakeRange(0, 2))`. The code is Swift 3 code. To change the attributes of particular substrings rather than ranges use `enumerateSubstrings(in:options:...` – vadian Apr 10 '17 at 10:21

2 Answers2

3

Enjoy - below code works for swift 3

        let normalText = "Hi am normal"

        let boldText  = "And I am BOLD!"

        let attributedString = NSMutableAttributedString(string:normalText)

        let attrs = [NSFontAttributeName : UIFont.boldSystemFont(ofSize: 15)]
        let boldString = NSMutableAttributedString(string:boldText, attributes:attrs)

        attributedString.append(boldString)

        txt.attributedText = attributedString

where txt is TextField outlet

Jack
  • 13,571
  • 6
  • 76
  • 98
  • While this code snippet may solve the question, [including an explanation](http://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – Jacob King Apr 10 '17 at 10:43
0
 assign "**string**" variable value to **attributedText** property

    var m_field = UITextField()
    m_field.frame = CGRect(x: 0, y: 66, width: 400, height: 100)
    let string = NSMutableAttributedString()
    string.bold("Bold_text: ",size: 15).normal("normal text")
    m_field.attributedText = string
    self.view .addSubview(m_field)

enter image description here

Shefali Soni
  • 1,234
  • 14
  • 26
  • While this code snippet may solve the question, [including an explanation](http://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – Jacob King Apr 10 '17 at 10:43