I'm a newcomer to text programming in iOS and a bit stumped at the moment. I am creating an example project to learn how to make a lolcat app (start with something fun!).
I have a UIImage with the cat and I'm putting some text on top of it by having a UITextView on top with text.
I'd like to end up with something like this for the text:
I was able to successfully pull this off when I had the text in a UILabel. I used code I found on SO that used drawTextInRect to accomplish the magic.
Now, I'd like to do the same thing in the UITextView (I want to be able to scroll the text).
**EDIT: ** so here is what I'm trying so far:
var foo = NSAttributedString(string: text, attributes: [
NSStrokeColorAttributeName : UIColor.blackColor(),
NSForegroundColorAttributeName : UIColor.whiteColor(),
NSStrokeWidthAttributeName : NSNumber(float: -4.0),
NSFontAttributeName : UIFont.systemFontOfSize(30.0)
])
That gets me a result like this:
If I now double the stroke width to -8.0 I get the below result which looks pretty darn bad in my estimation. It seems like there is a tradeoff between the stroke width and how much of the fill I can see. Ideally I'd like the stroke to just get wider without eating up the white text.
You need to read up on NSAttributedString. You can give letters an outline stroke:
https://developer.apple.com/library/ios/documentation/UIKit/Reference/NSAttributedString_UIKit_Additions/#//apple_ref/doc/constant_group/Character_Attributes
Use the NSStrokeColorAttributeName and NSStrokeWidthAttributeName to configure the outline stroke.
Once you have your attributed string you can put it an a label, put it in a text view, or draw it directly, as you please.
Might look a little better if you add some expansion and a tiny bit of shadow:
You can use the help of library DrawingLabel without any CoreGraphics code
DrawingStroke *blackStroke = [DrawingStroke new];
blackStroke.strokeWidth = 3.0;
blackStroke.strokeColor = [UIColor blackColor];
Related
I am using Custom Font in iOS application (Xamarin.Forms App). It is working fine but the text is clipped at the bottom of UILabel. It clearly cuts letter "g"
Custom Font: FuturaStd-Light.ttf
Actual Image In App:
Edited Image to ensure not height issue: Added Background color to Label to ensure it is not height issue.
I read here that to adjust ascender and descender property of Font. But still it doesn't help. Any help would be really appreciated.
Note: It is looking good if we remove custom font. The issue occurs only for the custom font that i am using.
you could try to use this:
NSMutableParagraphStyle paragraphStyle = new NSMutableParagraphStyle();
paragraphStyle.MinimumLineHeight = 24 + 1;//24 is your font size
NSMutableDictionary attributes = new NSMutableDictionary();//NSParagraphStyleAttributeName
attributes.SetValueForKey(paragraphStyle,new NSString("NSParagraphStyleAttributeName"));
label.AttributedText = new NSMutableAttributedString(label.Text,attributes);
This is a bit of an odd issue for me. I'm displaying some complex attributed text in a label and it was working well until recently a small grey line began to appear above certain lines of text like below:
Note that this is distinct from the tableview cell separator and only appears above the rect of the label.
With a bit of debugging, I also noticed the line doesn't appear when I don't have the little blue bracketed "flair" tag:
I am using the following code to add the problematic blue tags:
let captionFont = UIFont.preferredFont(forTextStyle: UIFontTextStyle.caption1);
let offset = (bodyFont.lineHeight / 2) - (captionFont.lineHeight / 2)
attributedString.addAttributes([NSAttributedStringKey.font : captionFont, NSAttributedStringKey.foregroundColor : Constants.linkColor, NSAttributedStringKey.baselineOffset:offset], range: flairRange!)
What am I doing wrong? Is this a CoreText bug?
After a bit of trial and error, I determined the issue only appeared when offset was a decimal value. When I simply replaced my offset line with
let offset = ceil((bodyFont.lineHeight / 2) - (captionFont.lineHeight / 2))
the line no longer appeared. I suspect this is a CoreText bug.
I'm new to Stackoverflow.
I am currently developing a mobile application using XCode for iOS.
However I'm trying to set add a white outline/stroke to my label but I do not know hot to. I have tried searching these forums but could not find any solution in swift.
I have tried using the shadow property, but it's not satisfactory.
How do I add an outline to my label in Swift?
Edit: The text should look like the text in this picture: http://cf.chucklesnetwork.com/items/7/5/7/4/4/original/yo-dawg-i-heard-you-like-captions-so-i-put-captions-of-captions.jpg
You need to use NSAttributedString, set strokeColor and strokeWidth to set outline and foregroundColor to set text color. Try this:
let attrString = NSAttributedString(
string: "Write Something with Outline",
attributes: [
NSAttributedStringKey.strokeColor: UIColor.black,
NSAttributedStringKey.foregroundColor: UIColor.white,
NSAttributedStringKey.strokeWidth: -2.0,
NSAttributedStringKey.font: UIFont.systemFont(ofSize: 17.0)
]
)
yourLabel.attributedText = attrString
This will look like below:
You can use this OutlinedLabel. Unlike most examples, it actually outlines the text.
And you can even gradient the outline.
Just set the outline color and the line width:
label.outlineColor = .white
label.outlineWidth = 7
This will look like this
I have 3 labels next to each other inside a horizontal stack view like this
The first and last label are going to be dynamically filled with a word of variable length, while the middle one is always going to contain the word "with", for example: "Coding with Swift!"
I don't want there to be extra space between each word as it would look strange if the words are short. Is there any way to make the labels only be as wide as they need to be to fit their text? That way it all looks like one label (except I'm making the middle label have smaller text).
Add 3 labels inside a UIView instead of stack view.
And set constraints as shown in image.
How about using autolayout?
You could give a horizontal space of 0 between the labels and a constraint of width for the middle one.
If u wish to have different fonts for the content in a label you can use NSAttributedString
let boldFontDict:[String: AnyObject] = [NSForegroundColorAttributeName : UIColor.black, NSFontAttributeName: <UI Font>]
let 1stString = NSAttributedString(string: "Hello")
let 2ndString = NSAttributedString(string: "hii", attributes: boldFOntDict)
let finalAttrStr = NSMutableAttributedString(attributedString: 1stString)
finalAttrStr.append(2ndString)
myLabel.attributedText = finalAttrStr
You can take one label instead of taking three label
and and set the text on label as
label.text= firstLabel.text + "with" + lastLabel.text
You can use autolayout to fix this problem.
I would like to implement the Medium iOS App like effect for tapping highlight and shows tooltip.
I have been researching on Text Kit and some other stackoverflow questions have some thoughts on it, please also suggest what's the better alternative to this.
Scenario:
Static Text pre-defined
Shows highlights in several words or phrases
Solution thoughts:
Use UITextView for storing text
Use attributed string for text content
Showing background color using NSBackgroundColorAttributedName
Detect the selection by layoutManager.characterIndexForPoint(...)
Shows tooltip next to the selection
Shows tooltip using one of these pods AMPopTip, CMPopTipView, EasyTipView
Right now, I am not able to select the word and shows the tooltip just next to it. Any tier of help is appreciated.
Here is a way to Highlight text.
Create an IBOutlet named myLabel
In ViewDidLoad type
let yourString = "This is how you highlight text"
myLabel.text = yourString
let malleableString = NSMutableAttributedString(string: yourString)
let numberOfCharactersToHighlight = 5
let startingIndex = 1
malleableString.addAttribute(NSBackgroundColorAttributeName, value: .magenta, range: NSRange(location: startingIndex, length: numberOfCharactersToHighlight))
myLabel.attributedText = malleableString