Format part of text in TextField - ios

I have implemented a text field in my iOS App and now I want a different formation for a passage of the text.
I find in the forum the suggestion to use the following code:
var temp:String = "Hello World"
let attrsBold = [NSFontAttributeName : UIFont.boldSystemFont(ofSize: 15)]
let txTeam1 = NSMutableAttributedString(string: temp, attributes: attrsBold)
What now happened is, that the text is not formatted but is written with the following information in the text field:
HelloWorld{NSFont =” font-familiy:
\”.SFUIText-Semibold\”;font-weight: bold; font-style: normal;
font-size: 15.00pt”;}
The text is set to the text field as follows:
textBericht.text = gameResult[0].bericht
The variable gameResult is written by a function:
gameResult = game.calculateGame(basic: gameBasic)
What I doing wrong? It seems the app is not interpreting the text after "Hello World".

You must set the text with
textBericht.attributedText = temp

Related

UITextView or UILabel with html hyperlinks

I need text with multiple tapeable links init.
In Android I just used simple html string for that
Example:
"Photo The owner / CC BY-SA 3.0"
And it worked only when I clicked on words corresponding links.
I spent a day and did not manage to find simple solution for ios.
I tried to use extension for UITextView :
extension UITextView {
func setHTMLFromString(text: String) {
let modifiedFont = NSString(format:"<span style=\"font-family: \(self.font!.fontName); font-size: \(self.font!.pointSize)\">%#</span>" as NSString, text)
let paragraph = NSMutableParagraphStyle()
paragraph.alignment = .center
let attrStr = try! NSAttributedString(
data: modifiedFont.data(using: String.Encoding.unicode.rawValue, allowLossyConversion: true)!,
options: [NSAttributedString.DocumentReadingOptionKey.documentType:NSAttributedString.DocumentType.html, NSAttributedString.DocumentReadingOptionKey.characterEncoding: String.Encoding.utf8.rawValue],
documentAttributes: nil)
self.attributedText = attrStr
}
But links work not only when I click on the hyperlink world but also when I click on simple text between. And I did not manage to align this text in textView(center) as well.
Does anybody know correct way?
Can't find documentation at the moment, but close observation indicates:
Because tapping with a finger is not as precise as clicking with a mouse pointer, the link gets a larger "tap region" than the text itself.
With your example - "Photo The owner / CC BY-SA 3.0" - I can tap on the ow characters without triggering either link. If my tap is on The it triggers the Photo link, if my tap is on new / it triggers that link.
Edit
One option to center the text...
In your extension, change
let modifiedFont = NSString(format:"<span style=\"font-family: \(self.font!.fontName); font-size: \(self.font!.pointSize)\">%#</span>" as NSString, text)
to
let modifiedFont = NSString(format:"<p style=\"text-align:center\"><span style=\"font-family: \(self.font!.fontName); font-size: \(self.font!.pointSize)\">%#</span></p>" as NSString, text)
added the <p style=... tag.

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.

What is UTF-8 character for "previous arrow", same as system back in UINavigationItem?

I just couldn't find utf-8 encoding for character like following:
I think that it is quite possible to create this in code, as a composed character from two dashes: / and \, but I do not know what characters to compose.
Maybe U+003C
print("\u{003C}") // <
Possibly U+2039.
print("\u{2039}") // ‹
Or U+276E.
print("\u{276E}") // ❮
I'm not certain if this will work for a navigation item, but you could make use of NSAttributedString to have string with different sizes of the substrings within it. This is described in detail in this great answer:
How do I make an attributed string using Swift?
As an example for your case:
let myAttributeBack = [ NSFontAttributeName: UIFont(name: "HelveticaNeue-UltraLight", size: 30.0)!]
let attrString = NSMutableAttributedString(string: "\u{276E} ", attributes: myAttributeBack)
let myAttributeText = [ NSFontAttributeName: UIFont(name: "HelveticaNeue-UltraLight", size: 15.0)!]
let backString = NSAttributedString(string: "back", attributes: myAttributeText)
attrString.appendAttributedString(backString)
myLabel.attributedText = attrString
However, as you write below, perhaps it's not possible to use attributed strings for title of a navigation bar item. Then I'd assume that the navigation bar example you showed above simply contains an image with the back bracket and a string "back" for the title text.
Is it this:
〈
The code is U+276C.
see https://en.wiktionary.org/wiki/%E3%80%88

Attributed Strings for UILabels in Xcode 6

I wanted to change the text of a label that says "hello world" such that "hello" is red, and "world" is blue. I'm doing this programmatically and obviously simplifying this example.
Is there a way to do this?
My alternative was to just create two uilabels, and set the attributes of each one accordingly, and then just add them together next to each other, but I thought it would be great if I could just assign the text of a label as per above instead.
By the way it doesn't have to be a UILabel as long as I can just render this text inside a UITableView row.
Thanks!
Here is a small example on how to use NSAttributedString:
var resultString = NSMutableAttributedString(string: "Hello ", attributes: [NSForegroundColorAttributeName : UIColor.redColor()])
var worldString = NSMutableAttributedString(
string: "World",
attributes: [
NSForegroundColorAttributeName : UIColor.redColor(),
NSFontAttributeName : UIFont.boldSystemFontOfSize(13.0)
]
)
resultString.appendAttributedString(worldString)
Then you can assign it to UILabel:
UILabel().attributedText = resultString

How to set iOS UITextField placeholder color? (RubyMotion + iOS)

How to use RubyMotion to set a textField's placeholder text foreground color?
Example code:
textField = UITextField.alloc.init
textField.placeholder = "Hello World"
The placeholder text shows up gray; I want it to be red.
This is using RubyMotion, not Objective-C.
Use an attributed string like this:
self.attributedPlaceholder =
NSAttributedString.alloc.initWithString(
self.placeholder || "Hello World",
attributes: {NSForegroundColorAttributeName => UIColor.redColor}
)
This works on iOS7, and likely on iOS6, and not on earlier iOS versions.
Careful: be certain to use the hash syntax key => val instead of key: val.

Resources