I am using custom font in my code. After using it, letters are not aligned in one line which is causing UI issues.
I want to change font or label setting so that it look like below :
Any help?
I think, it is due to the Font style so you can set the text as NSAttributedString String and manage the Font accordingly. In that case create two NSMutableAttributedString with different Attributesand use it.
extension String {
func getText() -> NSAttributedString {
let str1 = "Hello"
let str2 = "750"
//create attribute
let attStr1 = NSMutableAttributedString(string: str1)
let attFormat = [NSFontAttributeName : UIFont.systemFontOfSize(10.0)]
let attString = NSMutableAttributedString(string: str2, attributes: attFormat)
attString.appendAttributedString(attStr1)
return attString
}
}
Just create Attribute according to your requirement and it will work.
Related
I am trying to apply diff types of fonts on name of person example " Gopinath Puppala 123”. this is dynamic person name coming from service.
Here in name() any where any numbers of symbols might come. So i should apply system font only to see exactly it.
for Gopinath Puppala i should apply custom font. This should not impact on symbols. This custom font do not have symbols inside font.
Please suggest solution how to apply one font for symbols, one font for alphabets etc..
Thanks In Advance
Gopinath PUPPALA
Try this:
Extract symbolString and nameString separately and use in the code below
let labelString = NSMutableAttributedString()
let symbolString = NSAttributedString(string: symbolString : UIFont.systemFont(ofSize: 15.0)])
labelString.append(symbolString)
let nameString = NSAttributedString(string: nameString "CustomFontName", size: 11.0)])
labelString.append(nameString)
labelObject.attributedText = labelString
I'm using swift. Is there any way to change default font/ font size etc of an NSMutableAttributedString? Clearly I could set those values specifically for a given range - but that will then override any specific settings in the string.
You can use like that
let yourAttributes = [NSFontAttributeName: UIFont.preferredFontForTextStyle(UIFontTextStyleHeadline), NSForegroundColorAttributeName: UIColor.blackColor()]
let yourString = NSAttributedString(string: "Here my string !", attributes: yourAttributes)
Thanks
I am working on an app on which i had to make the text in a UITextView bold or italic or underlined. So i came up with this solution to make the selected range from the textView bold. The same way i can make it italic and but not underlined, don't know how.
//MARK: Bold Bar Button
func boldBarButtonClicked(sender : UIBarButtonItem)
{
if let selectedTextRange = self.textView.selectedTextRange
{
let beginningOfDocument = self.textView.beginningOfDocument
let startingPoint = self.textView.offsetFromPosition(beginningOfDocument, toPosition: selectedTextRange.start)
let selectedTextLength = self.textView.offsetFromPosition(selectedTextRange.start, toPosition: selectedTextRange.end)
let dict : Dictionary<String, AnyObject> = self.textView.textStorage.attributesAtIndex(startingPoint, effectiveRange: nil)
if let currentFont = dict[NSFontAttributeName] as? UIFont
{
let fontDescriptor = currentFont.fontDescriptor()
let changedFontDescriptor = fontDescriptor.fontDescriptorWithSymbolicTraits(UIFontDescriptorSymbolicTraits.TraitBold)
let updatedFont = UIFont(descriptor: changedFontDescriptor, size: 0.0)
let updatedDict = [NSFontAttributeName : updatedFont ]
self.textView.textStorage.beginEditing()
let selectedNSRange = NSMakeRange(startingPoint, selectedTextLength)
self.textView.textStorage.setAttributes(updatedDict, range: selectedNSRange)
self.textView.textStorage.endEditing()
// Put bold tag inside letters
}
}
}
So basically when the user sees the textView i have to make the text bold. So i thought of putting the text in between bold tag just the same as html. So that i can loop through the characters and see whether there is any text which is bold or italic or underline.
Am i doing it the correct way? Finding the selected range make it bold or italic or underlined and then put the tag inside the reference string, finally passing this string to the server with tags involved, so that others can see where comes the bold or italic or underlined characters.
How this will work if the same string has multiple attributes like, if the string "alvin" has bold and italic and underline, it will be like U I B "alvin' /U /I /B (tags are same as html bold, italic, underline) correct? How can i do find these in Swift, i thought about it a lot and tired of using regular expressions and looping through the string. But could not get the proper result. Thanks in advance.
I have a UILabel in my storyboard, and I have an #IBOutlet to it in my controller. In my viewDidLoad, I am setting its attributed text with two different font sizes.
let str1 = NSMutableAttributedString(string: "first", attributes: [NSFontAttributeName: UIFont.systemFontOfSize(15.0)])
let str2 = NSMutableAttributedString(string: "second", attributes: [NSFontAttributeName: UIFont.systemFontOfSize(10.0)])
str1.appendAttributedString(str2)
myLabel.attributedText = str1
Unfortunately, when I run the app, I can see the "firstsecond" string, but all in the same size (str1's 15-point font). Why is str2's 10-point font not being set?
Thanks in advance.
You have to use addAttribute(...) to apply multiple attributes to the same string.
let first = "first"
let second = "second"
let string = NSMutableAttributedString(string: first + second)
string.addAttribute(NSFontAttributeName, value: UIFont.systemFontOfSize(15), range: NSMakeRange(0, first.characters.count))
string.addAttribute(NSFontAttributeName, value: UIFont.systemFontOfSize(10), range: NSMakeRange(first.characters.count, second.characters.count))
myLabel.attributedText = string
The documentation for UILabel's attributedText property states:
assigning a new value updates the values in the font, textColor, and
other style-related properties so that they reflect the style
information starting at location 0 in the attributed string.
So whatever the style information is at location 0, that's what the label's going to be. That's why you're only seeing str1's 15 point font.
I'm using MMMarkdown and when I apply no font to UILabel or UITextView (I have to keep UITextView so TTTAttributedLabel won't do), all of markdown works. When I give the label or textview a font, I only get markdown hyperlinks to work. I tried switching to the AttributedMarkdown library but hyperlinks don't work at all with that one.
In my Markdown Class:
class Markdown: NSObject {
func markdownString(stringForVideoDescription:NSString) -> NSMutableAttributedString {
var options = [NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType]
var error:NSError?
var markdown:NSString = stringForVideoDescription
var html:NSString = MMMarkdown.HTMLStringWithMarkdown(markdown, error: &error)
var markdownAttributedString:NSMutableAttributedString = NSMutableAttributedString(data: html.dataUsingEncoding(NSUTF32StringEncoding)!, options: options, documentAttributes: nil, error: &error)!
if let font: UIFont = UIFont(name: "Marion-Regular", size: 14) {
markdownAttributedString.addAttributes([NSUnderlineStyleAttributeName:NSUnderlineStyleAttributeName], range: NSMakeRange(0, markdownAttributedString.length))
}
println(html) // used to see that markdown is in fact working
return markdownAttributedString
}
In my view, I'm actually pulling from json api but I replace to test with:
let bodyText = "\*This* sample \[I'm an inline-style link](https://www.google.com)"
myTextView.attributedText = Markdown().markdownString(bodyText)
Do I need to set up css to specify font for each bold, italicized text? Any help is greatly appreciated as my designers don't want to use the default font of Times New Roman.