Did anybody used digital-7 fonts in UILabel, i found text to align in bottom(aligns too low on the Y axis instead of being in the vertical center of the label). do anybody has solution to show them center aligned?
What you're asking about, with that particular font, is that it aligns too low on the Y axis instead of being in the vertical center of the label. Some solutions:
get a font manager and fix the font by hand
adjust the UILabel instances or a UILabel subclass to push it up higher than it is. You could do this by intercepting a setFrame call, for instance.
As far as I can see, UIFont does not carry that information with it at all. It might, though: you can check some of the read-only properties of the font.
With that said, however, you still can't set properties on a font, so you cannot adjust it in this way.
try this..
UILabel *testLabel = [[UILabel alloc] initWithFrame:CGRectMake(148, 142, 130, 25)];
[testLabel setFont:[UIFont fontWithName:#"DBLCDTempBlack" size:20]];
[testLabel setText:#"MY Text"];
[testLabel setTextColor:[UIColor darkGrayColor]];
[testLabel setBackgroundColor:[UIColor clearColor]];
[testLabel setTextAlignment:UITextAlignmentCenter];
I had used custom fonts in my project earlier. Just you have to add the font.tff file in your project and set your font with setFont method
Related
I am working on a product based application in which it shows product name with price increement or decreement.
As of now, according to my requirement I am inserting a UIImage as a subview to UILabel.But its everytime I need to calculate product name length on which I am defining x position of UIImage and adding it again and again.Product name is of variable size.
Not at all sometimes x position of image could not set properly so it overlaps text on UILabel.I stucked with this problem.
Below is my effort fired on such requirement.May be there should be another way to do such things but I don't know.Any other alternative I can apply?
int level=3;
NSString *product=#"Pea Price:";
UIImageView *imgView=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 20, 25)];
[imgView setImage:[UIImage imageNamed:#"up.png"]];
CGRect rect=[imgView frame];
UILabel *label=[[UILabel alloc] initWithFrame:CGRectMake(20, 70, 200, 30)];
label.backgroundColor=[UIColor whiteColor];
CGSize size=[product sizeWithFont:label.font constrainedToSize:CGSizeMake(MAXFLOAT, 30) lineBreakMode:NSLineBreakByTruncatingTail];
rect.origin.x=size.width-5;
[imgView setFrame:rect];
label.text=[NSString stringWithFormat:#"%# (%i%%)",product,level];
[label addSubview:imgView];
[self.view addSubview:label];
You can use like that
label.text=[NSString stringWithFormat:#"%# ⬆\uFE0E (%i%%)",product,level];
label.text=[NSString stringWithFormat:#"%# ⬇\uFE0E (%i%%)",product,level];
just copy and paste this arrow image to NSString.
Change foreColor of label will change the arrow color.
get Other Images to paste from here
Arrows -- http://www.alanwood.net/unicode/miscellaneous_symbols_and_arrows.html
Circular Digits -- http://www.alanwood.net/unicode/enclosed_alphanumerics.html
Images -- http://www.alanwood.net/unicode/miscellaneous_symbols.html
Smiles -- http://www.alanwood.net/unicode/emoticons.html
Miscallaneous -- http://www.alanwood.net/unicode/miscellaneous-symbols-and-pictographs.html
You could use Unicode characters for arrows (⬈ and ⬊) with NSAttributedStrings for colors, or even Emojis : ↗ and ↘ (view this page in the Simulator to see them).
The simplest way to achieve this is to use a UIWebView and insert your image as an HTML tag. But performance-wise, it's not great.
I want to make my label as shown in the image
I know I can get this effect by putting image view on it.
but is there any other method to do ?
How can I put line on label ?
Try this,
UILabel *blabel = [[UILabel alloc] initWithFrame:CGRectMake(XX, 6, 271, 26)];
blabel.text = #"Hellooooooo";
blabel.textAlignment = UITextAlignmentCenter;
blabel.backgroundColor = [UIColor clearColor];
blabel.textColor = [UIColor blackColor];
blabel.font = [UIFont systemFontOfSize:14];
[scrollDemo addSubview:blabel];
//underline code
CGSize expectedLabelSize = [#"Hellooooooo" sizeWithFont:blabel.font constrainedToSize:blabel.frame.size lineBreakMode:UILineBreakModeWordWrap];
UIView *viewUnderline=[[UIView alloc] init];
viewUnderline.frame=CGRectMake((blabel.frame.size.width - expectedLabelSize.width)/2, expectedLabelSize.height + (blabel.frame.size.height - expectedLabelSize.height)/2, expectedLabelSize.width, 1);
viewUnderline.backgroundColor=[UIColor blackColor];
[scrollDemo addSubview:viewUnderline];
[viewUnderline release];
The line above will appear below the text. You just need to change Y for UIView and it'll do wonders :)
put another label with "_" over it
transparent background.
you can create UIView with line's height and width and give background color to it. Put UIView over your UILabel .
For one of my projects I've created an UILabel subclass, which supports multiline text, underline, strikeout, underline/strikeout line offset, different text alignment and different font sizes.
Please see provided link for more info and usage example.
https://github.com/GuntisTreulands/UnderLineLabel
Place a UIImageView with line image on your label so when you run application it will fit.
At the moment I have a label being sized correctly using [aString sizeWithFont:constrainedToSize:lineBreakMode:] but I've introduced rotation into my device and with a flexible width and height this results in my UILabel stretching width ways (because its set relative to the main view) but not then contracting height-wise to compensate for the extra space.
In another question I asked I was told to use sizeToFit and/or sizeThatFits: however I can't find any useful resource on the internet that tells me how to use this appropriately and trials I've done result in irrational behavior.
In a nutshell, lets say I have a structure like this:
UIView *innerView = [[UIView alloc] initWithFrame:CGRectMake(10, 10, self.view.frame.size.width-20, 0)];
[innerView setAutoresizingMask:*flexible width and height*]
[innerView setBackgroundColor:[UIColor redColor]];
[self.view addSubview:innerView];
UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, innerView.frame.size.width-20, 0)];
[myLabel setAutoresizingMask:*flexible width and height*]
[myLabel setBackgroundColor:[UIColor blueColor]];
[myLabel setNumberOfLines:0];
[myLabel setLineBreakMode:UILineBreakModeWordWrap];
[innerView addSubview:myLabel];
Thats a simplified version (also by way of disclaimer, this isn't my acctual code, I realise these elements will leak and everything else... this is just an example so you can see the structure).
Basically I have those two elements. Now on rotation they will both stretch to fit the new view size. What I need is a way to increase or decrease the height to fit the content dynamically. [aString sizeWithFont...] is only static. So how would I use sizeToFit or sizeThatFits to make this dynamic?
Thank you.
Tom
Have you tried resetting the frame of your labels after the screen rotation? Check the value of interfaceOrientation in:
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
Then, set the label's frame accordingly for each view mode inside didRotateFromInterfaceOrientation.
Here's an example of resizing label frames for string length: http://cocoamatic.blogspot.com/2010/08/uilabel-dynamic-sizing-based-on-string.html
I have to set the font size and font family of a dynamically created textView, so that it gives same appearance as the one generated using Interface builder. Any idea how to do this?
UITextView *textView
...
[textView setFont:[UIFont fontWithName:#"ArialMT" size:16]]
You try this
[textView setFont:[UIFont systemFontOfSize:15]];
or
[textView setFont:[UIFont boldSystemFontOfSize:15]];
or you want to give fontname and size then you try this
[textView setFont:[UIFont fontWithName:#"arial" size:16]]
There is a property called font in UITextView,
#property(nonatomic, retain) UIFont *font
You can do like this:
yourTextView.font = builderTextView.font
may be I am too late but wanted to post my answer as its most relevant with current SDK.
I have used following line of code and work like charm in iOS 8.0
Swift Version:
self.textView.font = [UIFont systemFontOfSize:fontSize];
where fontSize is CGFloat Value.
In IB, select the text area and use the inspector to set the editability (it’s next to the text color). To set the font, you have to bring up the font window (command-T). It may help to have some text in the view when you change the font and size.
textView.font = UIFont(name: "Times New Roman", size: 20)
This worked for me (Swift 4 using in Swift Playgrounds on Xcode).
I have a grouped UITableView with black background color.
Thus the gray section headers with the white drop shadows are unreadable.
Next thing to know, the section height varies depending on language and section.
How to solve this the most easy way ?
If I implement viewForHeaderInSection I also need to implement heightForHeaderInSection, but the height varies (several sections with different title and different language => different text length/view height)
You need to dynamically determine the height of the cell, and set your label so that it autosizes itself.
Hope this link will be helpful to you.
All the best.
I had the same issue in 6.1. This worked for me:
- (void) viewDidLoad {
[super viewDidLoad];
[self.tableView setBackgroundView:nil];
self.tableView.backgroundColor = [UIColor blackColor];
[[UILabel appearance] setShadowColor:[UIColor clearColor]];
[[UILabel appearance] setTextColor:[UIColor lightGrayColor]];