I am trying to set the font of the label as per follow but it gives me zero CGSize for that.
UIFont *abbrFont = [UIFont fontWithName:#"Helvetica Cyrillic Bold_5" size:50]; //Helvetica Cyrillic Bold_5 added Custom Font
CGSize abbrSizeOfString = [_addbrTitle sizeWithFont:abbrFont];//_addbrTitle is a NSString
NSLog(#"%f %f",abbrSizeOfString.width,abbrSizeOfString.height); //Everytime Prints (0.000,0.000)
Help me to solve this.
Thank you.
Try this,
UIFont *abbrFont = [UIFont fontWithName:#"Helvetica" size:50]; //Helvetica Cyrillic Bold_5 added Custom Font
CGSize abbrSizeOfString = [_addbrTitle.text sizeWithFont:abbrFont];//_addbrTitle is a Label
NSLog(#"%f %f",abbrSizeOfString.width,abbrSizeOfString.height); //E
You can use this line to set the size for label..
[label setFont:[UIFont fontWithName:#"Helvetica" size:20.0]];
UILabel *_addbrTitle = [[UILabel alloc] init];
[_addbrTitle setText:#"Hello"];
UIFont *abbrFont = [UIFont fontWithName:#"Helvetica" size:50]; //Helvetica Cyrillic Bold_5 added Custom Font
[_addbrTitle setFont:abbrFont];
CGSize abbrSizeOfString = [_addbrTitle.text sizeWithFont:abbrFont];//_addbrTitle is a Label
NSLog(#"%f %f",abbrSizeOfString.width,abbrSizeOfString.height); //Everytime Prints (0.000,0.000)
Font name you have entered in the code does not exists in system. So its printing (0,0) every time. Enter a font name which exists in system it will print the size.
change your font name to some other built in font name then try
like this
yourLabel.font=[UIFont fontWithName:#"Helvetica" size:16];
Your font name is not current follow this IOS supporting fonts list and Font name
http://iosfonts.com/
NSURL *fontURL = [NSURL URLWithString:[[NSBundle mainBundle]pathForResource:#"your_font_name" ofType:#"TTF"]];
for example:
NSURL *fontURL2 = [NSURL URLWithString:[[NSBundle mainBundle]pathForResource:#"segoe_semi_bold" ofType:#"TTF"]];
NSURL *fontURL2 = [NSURL URLWithString:[[NSBundle mainBundle]pathForResource:#"segoe_semi_bold" ofType:#"TTF"]];
now make add url to array
NSArray *arrFont =[[NSArray alloc]initWithObjects:fontURL1,fontURL2, nil];
int result =CTFontManagerRegisterFontsForURLs((CFArrayRef)arrFont, kCTFontManagerScopeUser, nil);
if(result)
{
NSLog("Font Install successfully");
}
now you can get font
Related
cell.textLabel.font = [UIFont fontWithName:#"Helvetica-Bold" size:8.0];
Is it possible to pass the name of the font family and size dynamically to label text of the cell in iPhone?
NSString *selectorString =#"Helvetica-Bold";
cell.textLabel.font = [UIFont fontNamesForFamilyName:#"%#",selectorString];
Try this its helps you.
UILabel *titlelbl=[[UILabel alloc] initWithFrame:CGRectMake(80, 0, 150, 40)];
titlelbl.font = [UIFont fontWithName:#"Helvetica-Bold" size:17];
titlelbl.textAlignment=UITextAlignmentCenter;
titlelbl.textColor=[UIColor blackColor];
you can set like this
float fontSize = 14.0f;
NSString *selectorString =#"Helvetica-Bold";
cell.textLabel.font = [UIFont fontWithName:[NSString stringWithFormat:#"%#",selectorString] size:fontSize];
#define FONT_HELVETICA_BOLD #"Helvetica-Bold"
#define FONT_SIZE 14.of
cell. textLabel.font=[UIFont fontWithName:FONT_HELVETICA_BOLD size:FONT_SIZE];
This code works well to make a url appear in a UITextview
UITextView * descriptionTextView = [[UITextView alloc] initWithFrame:CGRectMake(10, 50, 300, 300)];
descriptionTextView.textColor = [UIColor whiteColor];
descriptionTextView.dataDetectorTypes = UIDataDetectorTypeLink;
descriptionTextView.font = [UIFont fontWithName:#"Helvetica" size:16];
descriptionTextView.backgroundColor = [UIColor clearColor];
descriptionTextView.text = #"Click to go to the google website, http://www.google.com ";
descriptionTextView.autocorrectionType = UITextAutocorrectionTypeNo;
descriptionTextView.keyboardType = UIKeyboardTypeDefault;
descriptionTextView.returnKeyType = UIReturnKeyDone;
descriptionTextView.editable = NO;
descriptionTextView.tag = 1;
[self.view addSubview:descriptionTextView];
The problem is that I the whole url I write appears, http://www.google.com
Is there a way I can use just a single word to contain the link? So the user can only see 'Goggle' written in blue and when they click that work it opens safari.
Many Thanks,
-Code
you can do it use a UIWebview
use
- (void)loadHTMLString:(NSString *)string baseURL:(NSURL *)baseURL;
and code the whole message then wrap the link to
[webView.loadHTMLString:#"Google" baseURL:nil];
I know this is super old thread. UITextView supports rich text/HTML. You should create NSAttributedString and assign it to attributedText property to manipulate with non plain text content. Like so:
NSString *htmlString = #"<your HTML code>";
textView.attributedText = [[NSAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUnicodeStringEncoding]
options:#{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
NSCharacterEncodingDocumentAttribute: #(NSUTF8StringEncoding)}
documentAttributes:nil
error:nil];
I have an app where I am setting the font for a UILabel programmatically as below
[label setFont:[UIFont fontWithName:fontName size:[fontSize intValue]]];
wherein fontName is a string type variable that hold font name such as "Helvetica"
and fontSize will hold the size of the font. This works fine for me.
Now I want to make this text "Bold" how can I do that?
boldSystemFontOfSize
works for system font. How can I achieve the same for user defined fonts ?
Thanks.
Try using #"Helvetica-Bold" as the fontName.
Try this,
NSArray *arr = [UIFont fontNamesForFamilyName:#"myFavoriteFont"];
NSString *fontName = [arr objectAtIndex:0]; //or [arr objectAtIndex:1], it depends
[label setFont:[UIFont fontWithName:fontName size:[fontSize intValue]]];
Try,
fontName = #"myFavouriteFont-Bold";
[label setFont:[UIFont fontWithName:fontName size:[fontSize intValue]]];
By using the the method fontWithName:size:, you can set the font name with style. Doc says that the fontName is the fully specified name of the font and this name incorporates both the font family name and the specific style information for the font.
I have a long text string (including \n newline characters) that I feed into a UILabel for display. The UILabel is dynamically set up to provide sufficient space for the text.
My code looks like this:
myText = [NSString stringWithFormat:#"%#some text: %# \n \n %#", myText, moreText1, moreText2];
NSLog(#"%#", myText);
myLabelSize = [vLabelText sizeWithFont:[UIFont fontWithName:#"Helvetica" size:(15.0)] constrainedToSize:cMaxLabelSize lineBreakMode:UILineBreakModeWordWrap];
UILabel *lBody = [[UILabel alloc] initWithFrame:CGRectMake(cFromLeft, vFromTop, vLabelSize.width, vLabelSize.height)];
lBody.font = [UIFont fontWithName:#"Helvetica" size:(15.0)];
lBody.lineBreakMode = UILineBreakModeWordWrap;
lBody.textAlignment = UITextAlignmentLeft;
lBody.backgroundColor = [UIColor cyanColor];
[myScrollView addSubview:lBody];
lBody.text = vLabelText;
My problem is that the text does not wrap, but truncates after the first line. The \n newlines are ignored.
Just found the problem, my numberOfLines was still at 1. After setting it to 0 it works fine.
May be it will be useful - since some lines are deprecated now
lBody.lineBreakMode = NSLineBreakByCharWrapping;
lBody.textAlignment = NSTextAlignmentLeft;
and of course - lBody.numberOfLines should be more than 1(1 is default value)
How can I use strike-through font in objective C??? More specifically in UITableViewCell
cell.textLabel.text = name;
cell.detailTextLabel.text = quantity ;
cell.XXX = ??
this is Marin, the author of the attributed strings chapter in "iOS6 by Tutorials".
Since iOS6 there is actually a native support for a bunch of different text attributes, including strike-trough.
Here's a short example, which you can use for your cell text label:
NSDictionary* attributes = #{
NSStrikethroughStyleAttributeName: [NSNumber numberWithInt:NSUnderlineStyleSingle]
};
NSAttributedString* attrText = [[NSAttributedString alloc] initWithString:#"My Text" attributes:attributes];
cell.textLabel.attributedText = attrText;
That's all. Good luck!
EDIT: This answer is out of date as of iOS 6. Please see the more recent answers below
There is not native support for strike-through or underline fonts. You have to draw the lines yourself over the views for the labels.
This is silly since the font inspector for IB has options to set strike-through and underline, but these are promptly ignored if you try to set them.
CGRect frame = sender.titleLabel.frame;
UILabel *strikethrough = [[UILabel alloc] initWithFrame:frame];
strikethrough.opaque = YES;
strikethrough.backgroundColor = [UIColor clearColor];
strikethrough.text = #"------------------------------------------------";
strikethrough.lineBreakMode = UILineBreakModeClip;
[sender addSubview:strikethrough];
here is how you strikethrough your label. But remember, it only works after ios 6.0
NSNumber *strikeSize = [NSNumber numberWithInt:2];
NSDictionary *strikeThroughAttribute = [NSDictionary dictionaryWithObject:strikeSize
forKey:NSStrikethroughStyleAttributeName];
NSAttributedString* strikeThroughText = [[NSAttributedString alloc] initWithString:#"Striking through it" attributes:strikeThroughAttribute];
strikeThroughLabel.attributedText = strikeThroughText;
1-Get the size of text which needs to strikethrough
CGSize expectedLabelSize = [string sizeWithFont:cell.titleLabel.font constrainedToSize:cell.titleLabel.frame.size lineBreakMode:UILineBreakModeClip];
2-Create an line and add it to the text
UIView *viewUnderline = [[UIView alloc] init];
viewUnderline.frame = CGRectMake(20, 12, expectedLabelSize.width, 1);
viewUnderline.backgroundColor = [UIColor grayColor];
[cell addSubview:viewUnderline];
[viewUnderline release];
Strikethrough is possible by using NSStrikeThroughAttributes and attributedText of UILabel. Here is the solution in Swift
let strikeThroughAttributes = [NSStrikethroughStyleAttributeName : 1]
let strikeThroughString = NSAttributedString(string: "Text of Label", attributes: strikeThroughAttributes)
strikeThroughLabel.attributedText = strikeThroughString
NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:#"Your String here"];
[attributeString addAttribute:NSStrikethroughStyleAttributeName
value:#2
range:NSMakeRange(0, [attributeString length])];
yourLabel.attributedText = attributeString;
Have you thought about finding your own strikethrough font and loading it yourself? It isn't that hard, just add the UIFont to your Info.plist file and put the name of the font in there. Then you can manually set the text to that new strikethrough font.
See this post on loading custom font.
Can I embed a custom font...
UIView *strikeView = [[UIView alloc] initWithFrame:ccr(0, 0, myLabel.bounds.size.width, 1)];
strikeView.backgroundColor = [UIColor redColor];
strikeView.center = ccp(myLabel.bounds.size.width/2, myLabel.bounds.size.height/2);
[myLabel addSubview:strikeView];
This will strike through the whole cell. In case you need it to look like completed task:
CGSize size = cell.contentView.frame.size; // you'll draw the line in whole cell.
UIView *line = [[UIView alloc] initWithFrame:CGRectMake(15,size.height / 2,size.width - 30, 1)];
line.backgroundColor = [UIColor grayColor]; // set your preferred color
[cell addSubview:line];
You may indicate some other value in CGRectMake instead of 15 - it's offset by X. In this case you'd better then decrease width by doubled value (in my case it's 15*2 = 30) in order it look nice.