Adding padding to NSMutableAttributedString in UITextView - ios

How do I add padding to a substring of UITextView that is a NSMutableAttributedString (shown here highlighted in yellow)?
I currently have the attributed text (image 2) without any padding around the attributed text, but need to add some padding to it (image 3), which is some spacing around the highlighted text only (not the entire UITextView.
Here is a close-up comparing how the NSMutableAttributedString with/without padding (this padding should not apply to the rest of the text which unattributed):
Here's how I'm currently getting the text from the UITextView (after finding the range of text I want to highlight and storing the attributed text in attributedText):
var highlightedText = NSMutableAttributedString(string: "this text is highlighted")
let highlightedRange = NSRange(location: 0, length: highlightedText.characters.count)
highlightedText.addAttribute(NSBackgroundColorAttributeName,
value: UIColor.yellow, range: highlightedRange)
attributedText.replaceCharacters(in: highlightedRange, with: highlightedText)

Related

How to give space between text and underline with background color with corner radius for Attributed String in iOS

I am doing iOS application. I have requirement like I have to show particular text with underline and background color with some corner radius in my app.
I have tried below, I am able to show background color and underline, but I am not getting exact my requirement.
I have to show much space between underline and iOS application text. Also I have to show yellow background color with some rounded coreners.
Here is my code
let text = "This is sample text for my iOS application this time"
let range = (text as NSString).range(of: "iOS application" )
let attributedString = NSMutableAttributedString(string: text)
attributedString.addAttribute(.underlineStyle,
value: NSUnderlineStyle.single.rawValue,
range: range)
attributedString.addAttribute(NSAttributedString.Key.backgroundColor, value: UIColor.yellow , range: range)
attributedString.addAttribute(NSAttributedString.Key.baselineOffset, value: 2 , range: range)
myLabel.attributedText = attributedString
Any suggestions?

Why does single line text have new line?

I have been using attributed text for UILabel of UITableViewCell.
Sometimes,even if text is single line but, text has new line.
My code is here
if notnullCheck(catchcopy){
//行間
let attributedText = NSMutableAttributedString(string: catchcopy!)
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.lineSpacing = 5
attributedText.addAttribute(NSParagraphStyleAttributeName, value: paragraphStyle, range: NSMakeRange(0, attributedText.length))
self.catchcopyLabel.attributedText = attributedText
}
self.catchcopyLabel.sizeToFit()
The text height is 33 when multi line text is.
The text height is 14 when single line text is.
But sometimes, the text height is 19 when single line text is.
when line height is 19,the text has new line.
What is this problem?
The following text is debug log.
(98.0, 14.0)
勤務地表記確認
(230.0, 19.0)
ケイサイカキンなしんこうぃあ 02
Both texts are also single line.But height is not same.
Assuming you are using 'HiraKakuProN-W6' font and its size is 14.
It's not a matter of new line but of Japanese space character(全角スペース).
If you delete Japanese space character, you will get height of 14.
I have encountered this kind of strangeness since many years ago,
so I think it's BUG of HiraXXXXX-XX font.

Can't get UILabel's text to stay on multiple lines

I have a UILabel that should display text on multiple lines in case that it's too long to stay on a single line. This how I set its parameters in interface builder:
But even by doing so, the text still gets truncated:
This is how I set the text at runtime:
let text = "left button pressed 5 seconds ago, you may want to press another button now"
let attributedText = NSMutableAttributedString(string: text)
attributedText.addAttribute(NSFontAttributeName, value: UIFont.boldSystemFontOfSize(statusLabel.font.pointSize), range: (text as NSString).rangeOfString("left"))
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.lineBreakMode = .ByWordWrapping
attributedText.addAttribute(NSParagraphStyleAttributeName, value: paragraphStyle, range: NSMakeRange(0, attributedText.length))
statusLabel.attributedText = attributedText
Like you see I even tried to add a paragraph style attribute to force the text to stay on multiple lines, but it doesn't work.
Check that you're setting the auto layout constraints so you have the top, leading and trailing spaces defined, but don't hookup a vertical height, the label will adjust itself based on the content.
Edit:

Adding underline attribute to partial text UILabel in storyboard

How can I underline partial text of UILabel using only storyboard? I am able to do this in code and I'm able to underline the entire text of a label, but not just one or two words in the string.
select the UILabel and go to Attribute Inspector section.
Change the text value from plain to Attributed .
Select the particular part of text which you want to Underline .
Note: If u want full text to be Underline select full text.
Now right click and change the font to Underline.
It will Underline the text
Steps:-
Go to TextEdit and create your UILabel text with underline.
Change UILabel text to Attributed (Attributed Selector).
Copy Underlined text and assign to UILabel.
Through code:
func underLineText(text: String)-> NSMutableAttributedString
let attributedText = NSMutableAttributedString(string: text)
attributedText.addAttribute(NSMutableAttributedString.Key.underlineStyle, value: NSUnderlineStyle.single.rawValue, range: NSRange(location: 0, length: attributedText.length))
return attributedText
}
//calling of func
yourLbl.attributedText = underLineText(text:yourLbl.text!)
List of other underline Types
NSMutableAttributedString.Key.underlineStyle = NSUnderlineStyle.single.rawValue
NSMutableAttributedString.Key.underlineStyle = NSUnderlineStyle.thick.rawValue
NSMutableAttributedString.Key.underlineStyle = NSUnderlineStyle.double.rawValue
NSMutableAttributedString.Key.underlineStyle = NSUnderlineStyle.patternDot.rawValue
NSMutableAttributedString.Key.underlineStyle = NSUnderlineStyle.patternDash.rawValue
NSMutableAttributedString.Key.underlineStyle = NSUnderlineStyle.patternDashDot.rawValue
NSMutableAttributedString.Key.underlineStyle = NSUnderlineStyle.patternDashDotDot.rawValue
NSMutableAttributedString.Key.underlineStyle = NSUnderlineStyle.byWord.rawValue

Swift - Changing font size inside label

I have a label where there is text which should be bold and with another font size. Is there any possibility to do it like the line break ("Hello \n World!") with a command or do I have to make another label for this?
Thanks!
Look at the API for NSAttributedString -- it allows you to create a string that specifies portions of the string that should be styled with specific text styles and/or fonts. The resulting object can be used instead of a plain string with UILabel (and other UI elements) by setting the label's attributedText property instead of the usual text property.
To make just the word "bold" appear in 18 point bold, try something like the following:
var label = UILabel()
let bigBoldFont = UIFont.boldSystemFontOfSize(18.0)
var attrString = NSMutableAttributedString(string: "This text is bold.")
attrString.addAttribute(kCTFontAttributeName, value: bigBoldFont, range: NSMakeRange(13, 4))
label.attributedText = attrString
The range specified determines the portion of the string to which the named attributed (in this case, the font) should be applied. And note that the parameters to NSMakeRange are the starting character position and the length of the range.

Resources