-1

This is driving me nuts, I cant find a simple straightforward answer anywhere on how to make a text range bold, underlined with a custom font in Swift 4.

Every page I go to has a portion of the information but it is sending me round in spirals. I find the underline method for Swift 3, doesn't include custom font, the next one says to use the bold font custom version in Swift 4, etc but doesn't include underline. Very frustrating.

So. I have this text

Reset password email has been sent. Didn’t get it? Send again

'Send again' needs to be bold and underlined. Its a complete string, in one UILabel.

Pretty sure the new method uses UIAttributedStringKey.

I know its pretty simple to be fair which is why I've found it frustrating and have lost my patience with it.

Oh, I'm currently on iOS 11, XCode 9.4.1. I am probably going to update in the next couple of days.

Thanks in advance

Edit

Hey dfd, thanks for your comments. This is basically the code I've got (useless in swift 4.0 although working in Swift 3.0 pretty much as I remember using the code perviously.

I've got to declare the font somewhere and also I haven't found code for the bold either but I do have some code for the range and the underline.

Cheers

var text = self.completeText.text
let textRange = NSMakeRange(50, (text?.count)!)
let attributedText = NSMutableAttributedString(string: text!)
let underlineAttribute = [NSUnderlineStyleAttributeName: NSUnderlineStyle.StyleSingle.rawValue]
attributedText.addAttribute(underlineAttribute)

* Final Edit*

Heres working code.

let label = self.completeText
let labelString = self.completeText.text
let attributedText = NSMutableAttributedString(string: labelString!)
let rangeToUnderline = (labelString! as NSString).range(of: "Send again")
attributedText.addAttribute(NSAttributedStringKey.underlineStyle, value: NSUnderlineStyle.styleSingle.rawValue, range: rangeToUnderline)

I'm going to add the OpenSansBold custom font with these attrs and it should work ok.

Thanks Adrian, thanks for the suggestions everyone else. Much appreciated!

P.S I don't suppose anyone has got a decent reference for attributed strings on Swift 4.1? Would be nice.

  • It likely **is not** a Swift 4 issue, but rather something to *how* you are coding things. (BTW, could you supply that? What you've tried, what isn't working, and learn to use SO Markdown. Thanks.) Let's go with what I've gleaned from your near rant.... Yes, you have the right idea - use an `NSAttributedString`. Don't worry about the actual font, let's just do the default system font. You really didn't use markdown, so I'm guessing you want something starting with *Reset...*, ending with *...Send Again.* And...??? Something in between is underlined. Now that you've created that.... –  Oct 11 '18 at 01:18
  • .... it's time to try your custom font. But only then! (And if I'm way off s to your issue, apologies, you were on rant with no code to let me help you.) More details - it sounds like you re using `UILabel`, and wish to populate `UIAttributedStringKey`? Ae you sure it isn't `attributedText`? I'm asking because you didn't provide any code for what you tried. These details matter! Thanks. –  Oct 11 '18 at 01:21

2 Answers2

1

If you're not seeing your Open Sans, that's a separate issue. To underline it, you'd make use of an NSMutableAttributedString (not NSAttributedString).

Here's how you'd underline a portion of your string:

let label = UILabel()
let labelString = "Didn’t get it? Send again"
let attributedText = NSMutableAttributedString(string: labelString)
let rangeToUnderline = (labelString as NSString).range(of: "Send again")
attributedText.addAttribute(NSAttributedString.Key.underlineStyle, value: NSUnderlineStyle.single.rawValue, range: rangeToUnderline)

As for bolding a portion of the string, here's how you'd do that. I'm too lazy to track down the font names, test it, etc., so I'll leave it to you to fill in the bolding blank and the logic behind toggling between bold and non-bold.

Adrian
  • 16,233
  • 18
  • 112
  • 180
  • Yeah. I guess that was obvious! Thanks for pointing that out, I was so caught up in it not working I didn't notice the details of what I'd copied and pasted. Nice succinct answer thanks – Jack Spacie Oct 11 '18 at 02:09
  • Ok I've got an error NSAttributedString has no member 'key' – Jack Spacie Oct 11 '18 at 02:12
  • I'm on my Mojave partition using Xcode 10, so the syntax might be slightly different. `NS`-prefixed stuff tends not to change much (aside from go away) . If I answered your question, please click the checkmark – Adrian Oct 11 '18 at 02:14
  • This is what is frustrating because I'm pretty sure thats the right way to do it which I used before and in Swift 4.0 keys are the thing. Unless I've done something I shouldnt have. – Jack Spacie Oct 11 '18 at 02:17
  • 1
    Right, job done, no doubt when I upgrade later this week I'll have to go back to your code, but you've sorted my problem, thanks a lot – Jack Spacie Oct 11 '18 at 02:20
0

On the label Attributed Inspector change the text from Plain to Attributed. Then you can edit the text and add underline and even change font to bold.

enter image description here

Zaid M. Said
  • 314
  • 1
  • 15
  • The OP wants to toggle it after a condition is met. – Adrian Oct 11 '18 at 01:55
  • Ok that is an obvious way to do it, which I've tried but unfortunately it spews out my custom font and wont let me modify it as OpenSans (which is declared in the PList). Pretty surprised Apple hasn't added custom fonts in the attributed strings menu there by now. I could put the custom font in code and then modify the bold and underline there but, I think the GUI in IB would overwrite it with system or the OpenSans would overwrite the edits. – Jack Spacie Oct 11 '18 at 02:16
  • @JackSpacie the GUI in IB will load first and any custom font code will override IB – Zaid M. Said Oct 11 '18 at 02:22