Example of NSAttributedString with two different font sizes?
You would do something like this…
NSMutableAttributedString *hogan = [[NSMutableAttributedString alloc] initWithString:@"Presenting the great... Hulk Hogan!"];
[hogan addAttribute:NSFontAttributeName
value:[UIFont systemFontOfSize:20.0]
range:NSMakeRange(24, 11)];
This will set the last two words in 20-point text; the rest of the string will use the default value (which I believe is 12 points). The thing that might be confusing about setting the text size is that you have to set the typeface and the size at the same time—each UIFont
object encapsulates both of those properties.
NSMutableAttributedString with different fonts
I guess that your code work, but you think that it doesn't because font sizes look pretty much equally.
Here is what I see with your code
And that's what I see when I change size to 2
and 37
And that's your original sizes (7
and 17
), but for both strings, I've set the same text.
Is it possible to set multiple font attributes using an NSAttributedString
There is nothing wrong with your code. Possibly you are trying to use font names that don't exist. Did you check that the fonts actually exist, e.g. by po or NSLog?
What's the output if you log the attributedString?
Center two fonts with different different sizes vertically in an NSAttributedString
I'd say the easiest thing to do is just manipulate the NSBaselineOffsetAttributeName
attribute for the text in question:
NSBaselineOffsetAttributeName
The value of this attribute is an NSNumber object containing a floating point value indicating the character’s offset from the baseline, in points. The default value is 0.
To center, you'd take the difference between height of the large text and the height of the smaller text and halve it, then use that as the baseline adjustment.
How to add two different font size to textview text in swift
You can do that using attributedText.
try like this
var heading = "Bills or Taxes once paid through the payment gateway shall not be refunded other then in the following circumstances:"
var content = "\n \n 1. Multiple times debiting of Consumer Card/Bank Account due to ticnical error excluding Payment Gateway charges would be refunded to the consumer with in 1 week after submitting complaint form. \n \n 2. Consumers account being debited with excess amount in single transaction due to tecnical error will be deducted in next month transaction. \n \n 3. Due to technical error, payment being charged on the consumers Card/Bank Account but the Bill is unsuccessful."
let attributedText = NSMutableAttributedString(string: heading, attributes: [NSAttributedStringKey.font: UIFont.boldSystemFont(ofSize: 20)])
attributedText.append(NSAttributedString(string: content, attributes: [NSAttributedStringKey.font: UIFont.SystemFont(ofSize: 15), NSAttributedStringKey.foregroundColor: UIColor.blue]))
refundTextview.attributedText = attributedText
How to set font size on NSAttributedString
let myString = "Swift Attributed String"
let myAttribute = [ NSForegroundColorAttributeName: UIColor.blue ]
let myAttrString = NSAttributedString(string: myString, attributes: myAttribute)
// set attributed text on a UILabel
myLabel.attributedText = myAttrString
Font
let myAttribute = [ NSFontAttributeName: UIFont(name: "Chalkduster", size: 18.0)! ]
Shadow
let myShadow = NSShadow()
myShadow.shadowBlurRadius = 3
myShadow.shadowOffset = CGSize(width: 3, height: 3)
myShadow.shadowColor = UIColor.gray
let myAttribute = [ NSShadowAttributeName: myShadow ]
Underline
let myAttribute = [ NSUnderlineStyleAttributeName: NSUnderlineStyle.StyleSingle.rawValue ]
Textcolor
let myAttribute = [ NSForegroundColorAttributeName: UIColor.blue ]
Background Color
let myAttribute = [ NSBackgroundColorAttributeName: UIColor.yellow ]
Change only fontsize of NSAttributedString
If you only want to change the size of any given font found in the attributed string then you can do:
let newStr = someAttributedString.mutableCopy() as! NSMutableAttributedString
newStr.beginEditing()
newStr.enumerateAttribute(.font, in: NSRange(location: 0, length: newStr.string.utf16.count)) { (value, range, stop) in
if let oldFont = value as? UIFont {
let newFont = oldFont.withSize(20) // whatever size you need
newStr.addAttribute(.font, value: newFont, range: range)
}
}
newStr.endEditing()
print(newStr)
This will keep all other attributes in place.
If you want to replace all fonts in a given attributed string with a single font of a given size but keep all other attributes such as bold and italic, see:
NSAttributedString, change the font overall BUT keep all other attributes?
Related Topics
Xcode Is Looking for Core Data Entity Names with Dot; Not Compiling
Foreign Key Relationship Mapping with Restkit
Linker Command Failed with Exit Code 1 (Use -V to See Invocation), Xcode 8, Swift 3
Invalid Swift Support/Invalid Implementation of Swift
How to Log a Method's Execution Time Exactly in Milliseconds
Custom Installed Font Not Displayed Correctly in Uilabel
Using Multiple Storyboards in iOS
How to Create Layout Constraints Programmatically
One Step Affine Transform for Rotation Around a Point
Iphone: Hide Uitableview Search Bar by Default
iOS App Error - Can't Add Self as Subview
Static Table View Outside Uitableviewcontroller
Remove Gradient Background from Uiwebview
iOS 7 -- Navigationcontroller Is Setting the Contentinset and Contentoffset of My Uiscrollview
Ios: Sample Code for Simultaneous Record and Playback
iOS Uinavigationbar Button Remains Faded After Segue Back
Record and Play Audio Simultaneously
What's the Best Way to Find the User's Documents Directory on an Iphone