Using MMMarkdown, only system font will show bold or italicized text - ios

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.

Related

Change only fontsize of NSAttributedString

I have a NSAttributedString that was loaded from a RTF file, so it already holds several font-attributes for different ranges.
Now I want to adapt the font size to the screensize of the device, but when I add a whole new font attribute with a new size, the other fonts disappear.
Is there a way to change only the font size for the whole string?
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?

PDFPage.attributedString returns the wrong font

I'm reading an existing PDF using PDFKit. I get an attributedString for a page, but the fonts in the string don't match the fonts actually in the PDF:
The fonts in the PDF (according to several different apps) are:
CourierFinalDraft (TypeType Roman) Embedded Subset
CourierFinalDraft-Bold (TrueType Roman) Embedded Subset
CourierFinalDraft-Italic (TrueType Roman) Embedded Subset
My Swift code to get the font is:
guard let page = pdf.page(at: pageNo) else { return }
guard let content = page.attributedString else { return }
content.enumerateAttributes(in: range, options:[]) {(dict: [String:Any], range: NSRange, stop: UnsafeMutablePointer<ObjCBool>) -> Void in
let font = dict[NSFontAttributeName] as! UIFont
}
All of the text is returned in a single range. The font returned has the following:
font-family: "Times New Roman"
font-name: "TimesNewRomanPSMT"
So the Bold and Italic text are returned in the same range as the normal text, and I can't distinguish among them, which is what I'm trying to do. As well as the font name I also look at:
font.fontDescriptor.symbolicTraits.contains(.traitItalic)
but of course this is always False as all text is returned in the same range as if it is normal.
This is using the XCode simulator, in case that's relevant. The PDF does render correctly (including bold and italics) on Safari in the Simulator. Unfortunately I can't try it out on a real iPhone.
This question is a few years old, but I found that I had to make a mutable copy of the data in order to interact with it and then use the information available to modify the underlying font and re-display it.
let str = content.mutableCopy() as! NSMutableAttributedString
str.beginEditing()
str.enumerateAttribute(.font,
in: NSRange(location: 0, length: str.length),
options: []) { value, range, _ in
guard let value = value as? UIFont else {
return
}
let isBold = value.fontDescriptor.symbolicTraits.contains(.traitBold)
let font = // Your font logic here based on traits
str.removeAttribute(.font, range: range)
str.addAttribute(.font, value: font, range: range)
str.addAttribute(.paragraphStyle, value: style, range: range)
}
str.endEditing()
I have not gone over performance issues on this yet for larger pdfs, but it does not seem to take up too many resources for pdfs between 10-20 pages. For a higher number of pages, I would most likely create an actual paging mechanism and then adjust for each page on the fly rather than loop the entire pdf file.

Manage alignment in custom font in iOS

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.

String with multilpe attributes in swift

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.

Attributed string with custom fonts in storyboard does not load correctly

We are using custom fonts in our project. It works well in Xcode 5. In Xcode 6, it works in plain text, attributed string in code. But those attributed strings set in storyboard all revert to Helvetica when running on simulator or device, although they look all right in storyboard.
I'm not sure if it's a bug of Xcode 6 or iOS 8 SDK, or the way to use custom fonts is changed in Xcode 6 / iOS 8?
The simplest answer that worked for is to drag the fonts into FontBook. If the fonts are in your project but not in your computer's FontBook, IB sometimes has trouble finding it. Weird, but has worked for me on several occasions.
The fix for me was to use an IBDesignable class:
import UIKit
#IBDesignable class TIFAttributedLabel: UILabel {
#IBInspectable var fontSize: CGFloat = 13.0
#IBInspectable var fontFamily: String = "DIN Light"
override func awakeFromNib() {
var attrString = NSMutableAttributedString(attributedString: self.attributedText)
attrString.addAttribute(NSFontAttributeName, value: UIFont(name: self.fontFamily, size: self.fontSize)!, range: NSMakeRange(0, attrString.length))
self.attributedText = attrString
}
}
Giving you this in the Interface Builder:
You can set up your attributedstring just as you normal do, but you'll have to set your fontsize and fontfamily once again in the new available properties.
As the Interface Builder is working with the custom font by default, this results in a what you see is what you get, which I prefer when building apps.
Note
The reason I'm using this instead of just the plain version is that I'm setting properties on the attributed label like the linespacing, which are not available when using the plain style.
You can add custom fonts to font book.
Step1: Click on manage fonts. It opens the font book.
Step2: Click on plus and add your fonts.
Next time when you click on font with attributed text newly added font also will show in the list. But make sure your custom font added in info.plist and bundle resources.
My solution is a bit of a work around. The real solution is for apple to fix Interface Builder.
With it you can mark all the bold and italic text in interface builder using a system font, then at runtime render your custom font. May not be optimal in all cases.
NSMutableAttributedString* ApplyCustomFont(NSAttributedString *attributedText,
UIFont* boldFont,
UIFont* italicFont,
UIFont* boldItalicFont,
UIFont* regularFont)
{
NSMutableAttributedString *attrib = [[NSMutableAttributedString alloc] initWithAttributedString:attributedText];
[attrib beginEditing];
[attrib enumerateAttribute:NSFontAttributeName inRange:NSMakeRange(0, attrib.length) options:0
usingBlock:^(id value, NSRange range, BOOL *stop)
{
if (value)
{
UIFont *oldFont = (UIFont *)value;
NSLog(#"%#",oldFont.fontName);
[attrib removeAttribute:NSFontAttributeName range:range];
if([oldFont.fontName rangeOfString:#"BoldItalic"].location != NSNotFound && boldItalicFont != nil)
[attrib addAttribute:NSFontAttributeName value:boldItalicFont range:range];
else if([oldFont.fontName rangeOfString:#"Italic"].location != NSNotFound && italicFont != nil)
[attrib addAttribute:NSFontAttributeName value:italicFont range:range];
else if([oldFont.fontName rangeOfString:#"Bold"].location != NSNotFound && boldFont != nil)
[attrib addAttribute:NSFontAttributeName value:boldFont range:range];
else if(regularFont != nil)
[attrib addAttribute:NSFontAttributeName value:regularFont range:range];
}
}];
[attrib endEditing];
return attrib;
}
Inspired by this post
Thanks to this thread, I've come to this solution:
private let fontMapping = [
"HelveticaNeue-Medium": "ITCAvantGardePro-Md",
"HelveticaNeue": "ITCAvantGardePro-Bk",
"HelveticaNeue-Bold": "ITCAvantGardePro-Demi",
]
func switchFontFamily(string: NSAttributedString) -> NSAttributedString {
var result = NSMutableAttributedString(attributedString: string)
string.enumerateAttribute(NSFontAttributeName, inRange: NSRange(location: 0, length: string.length), options: nil) { (font, range, _) in
if let font = font as? UIFont {
result.removeAttribute(NSFontAttributeName, range: range)
result.addAttribute(NSFontAttributeName, value: UIFont(name: fontMapping[font.fontName]!, size: font.pointSize)!, range: range)
}
}
return result
}
I have struggled with this bug: UILabel displays correctly in IB with custom font but does not display correctly on device or simulator (font is included in the project and is used in plain UILabels).
Finally found Attributed String Creator on (Mac) App Store. Generates code to be placed in your app in the appropriate place. Fantastic.
I am not the creator, just a happy user.
Met the same problem: the attribute font set for TextView in storyboard didn't work in run time with XCode 6.1 & iOS 8 SDK.
This is how I solved this issue, might be a workaround for you:
open the attribute inspector of your textview, change text to
"Plain"
click on the cross to delete the "wC hR"(red below)
change text to "Attributed", and then you can set the font and size
for your text.
run to check if it works
Try this it will work
In my case when i try to set "Silversky Technology" as Attributed text for label from interface builder its not show when i run in simulator but its show in interface builder. So i used one trick i made Silversky font with 1 pixel bigger then Technology text.
Attribute text have issue with same size of font so change size of 1 word this thing work with me.
May be this is xcode bug but this trick work for me.
Met the same problem: the attribute font for UILabel in storyboard didn't work in run time. Using this UIFont+IBCustomFonts.m works for me
https://github.com/deni2s/IBCustomFonts
The same problem.
Solved: Just check Selectable in TextView. Without this i have standard System font.
Double click and install the font to the system. It will work (Xcode 8.2)
#Hamidptb solution works, make sure to get the correct name of the font (once you've added it to Font Book)
Open the Font Book application, navigate to your font then press Command+I. The PostScript name is the font name you want to use here:
UILabel.appearance().font = UIFont(name: "PostScriptName", size: 17)
I was trying to get tableView cells with text having multiple paragraphs. The attributed strings seemed to be a way to get extra space between the paragraphs (something a bit nicer looking than doing two line-feeds in the string). Came across this and other posts when I discovered that the IB settings didn't apply at run time when you wanted to put different text in the cell.
The main thing I came up with was adding an extension to String (using Swift) to
create an attributed string with certain characteristics. Example here uses the Marker Felt font, as it is easily distinguishable from Helvetica. The example also shows a little extra bit of spacing between paragraphs to make them more distinct from each other.
extension String {
func toMarkerFelt() -> NSAttributedString {
var style = NSMutableParagraphStyle()
style.paragraphSpacing = 5.0
let markerFontAttributes : [NSObject : AnyObject]? = [
NSFontAttributeName : UIFont(name: "Marker Felt", size: 14.0)!,
NSParagraphStyleAttributeName: style,
NSForegroundColorAttributeName : UIColor.blackColor()
]
let s = NSAttributedString(string: self, attributes: markerFontAttributes)
return s
}
}
Then, in my custom tableViewCell, you send it the text you want and it converts it to an attributed string on the UILabel.
// MarkerFeltCell.swift
class MarkerFeltCell: UITableViewCell {
#IBOutlet weak var myLabel: UILabel!
func configureCellWithString(inputString : String) {
myLabel.attributedText = inputString.toMarkerFelt()
}}
In the view controller with the tableView, you should register your cell in viewDidLoad() -- I used a nib, so something like:
let cellName = "MarkerFeltCell"
tableView.registerNib(UINib(nibName: cellName, bundle: nil), forCellReuseIdentifier: cellName)
To get the cell to figure out how tall it should be, make a prototype cell that is used to get size info, and is never added into the tableView. So, in your
view controller's variables:
var prototypeSummaryCell : MarkerFeltCell? = nil
Then in (probably override - depending on your view controller) heightForRowAtIndexPath:
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
// ...
if xib == "MarkerFeltCell" {
if prototypeCell == nil {
prototypeCell = tableView.dequeueReusableCellWithIdentifier(xib) as? MarkerFeltCell
}
let width : CGFloat = tableView.bounds.width
let height : CGFloat = prototypeCell!.bounds.height
prototypeCell?.bounds = CGRect(x: 0, y: 0, width: width, height: height)
configureCell(prototypeCell!, atIndexPath: indexPath)
prototypeSummaryCell?.layoutIfNeeded()
let size = prototypeSummaryCell!.contentView.systemLayoutSizeFittingSize(UILayoutFittingCompressedSize)
let nextHeight : CGFloat = ceil(size.height + 1.0)
return nextHeight
} else { // ...
In the above code, the prototypeCell will be filled in the first time it is needed. The prototypeCell is then used to figure out the height for the cell after going through the autosizing process. You will need to round up the height with the ceil() function. I also added in some extra fudge factor.
The final code bit is how you configure the text for the cell. For this example, simply:
func configureCell(cell :UITableViewCell, atIndexPath indexPath: NSIndexPath) {
if let realCell = cell as? MarkerFeltCell {
realCell.configureCellWithString("Multi-line string.\nLine 2.\nLine 3.") // Use \n to separate lines
}
}
Also, here is a shot of the nib. Pinned the label to the edges of the cell (with margin desired), but used a "Greater Than or Equal" constraint, with a less than "Required" priority for the bottom constraint.
Set the label's font to Attributed. Actual IB font didn't matter.
The result in this case:
In case of attributed string you can add custom font in font list as -
Click on font icon this will display following dialog .In the following dialog you can add your own category or existing one for custom font.attributed font dialog
After it click on Manage Fonts it open the following dialog select category in you created or existing one . Click on + sign to add font in the category.
Manage font dialog
that's have a simple and quick solition and that's work in my case .
that solution is add a code line in didFinishLaunchingWithOptions func in AppDelegate.swift file :
for textViews :
UITextView.appearance().font = UIFont(name: "IranSans", size: 17)
for labels :
UILabel.appearance().font = UIFont(name: "IranSans", size: 17)
and for rest of UiView like this two ☝️
For anyone applying custom fonts to attributed string in code: Try setting it in viewDidLayoutSubviews. My mistake was doing it in viewDidLoad, it won't be applied there.

Resources