How to remove empty white space before and after some images that are small.
Some images are working well as the example below and some are with huge space above and below the image.
Am using Parse so I think I can't access imageView directly :
// upload Image To Parse as PFImageView
let imgFile = object?.objectForKey("ImgView") as? PFFile
cell.ImgView.file = imgFile
cell.questionImgView.loadInBackground()
// cell.questionImgView.clipsToBounds = true
cell. cell.ImgView.clipsToBounds.contentMode = UIViewContentMode.ScaleAspectFit
You can simply do the following to your UIButton to give it multiple lines and different fonts:
NSMutableParagraphStyle *style = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
[style setAlignment:NSTextAlignmentCenter];
[style setLineBreakMode:NSLineBreakByWordWrapping];
UIFont *font1 = [UIFont fontWithName:#"HelveticaNeue-Medium" size:20.0f];
UIFont *font2 = [UIFont fontWithName:#"HelveticaNeue-Light" size:20.0f];
NSDictionary *dict1 = #{NSUnderlineStyleAttributeName:#(NSUnderlineStyleSingle),
NSFontAttributeName:font1};
NSDictionary *dict2 = #{NSUnderlineStyleAttributeName:#(NSUnderlineStyleNone),
NSFontAttributeName:font2};
NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] init];
[attString appendAttributedString:[[NSAttributedString alloc] initWithString:#"LINE 1\n" attributes:dict1]];
[attString appendAttributedString:[[NSAttributedString alloc] initWithString:#"line 2" attributes:dict2]];
[[self buttonToStyle] setAttributedTitle:attString forState:UIControlStateNormal];
[[[self buttonToStyle] titleLabel] setNumberOfLines:0];
[[[self buttonToStyle] titleLabel] setLineBreakMode:NSLineBreakByWordWrapping];
Source: iOS NSAttributedString on UIButton
Related
As question title, I want to truncate a part of NSAttriutedString.
Here's my code.
UIButton *btnWelcome = [UIButton buttonWithType:UIButtonTypeCustom];
[btnWelcome setContentMode:UIViewContentModeCenter];
[btnWelcome.titleLabel setNumberOfLines:1];
NSMutableParagraphStyle *titleTextParagrahStyle = [[NSMutableParagraphStyle alloc] init];
NSMutableParagraphStyle *specificNameParagrahStyle = [[NSMutableParagraphStyle alloc] init];
[titleTextParagrahStyle setAlignment:NSTextAlignmentCenter];
[specificNameParagrahStyle setAlignment:NSTextAlignmentCenter];
[specificNameParagrahStyle setLineBreakMode:NSLineBreakByTruncatingTail];
NSDictionary *titleTextAttributes = #{NSForegroundColorAttributeName:[UIColor whiteColor], NSFontAttributeName:[UIFont boldSystemFontOfSize:17.0f], NSParagraphStyleAttributeName:titleTextParagrahStyle};
NSDictionary *specificNameAttributes = #{NSForegroundColorAttributeName:[UIColor blueColor], NSFontAttributeName:[UIFont boldSystemFontOfSize:17.0f], NSParagraphStyleAttributeName:specificNameParagrahStyle};
NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:#("Welcome To '%#' World!, 'VeryVeryLongSpeicificName'] attributes:titleTextAttributes];
NSRange specificNameRange = [attrString.string rangeOfString:[NSString stringWithFormat:#"'%#'", #"VeryVeryLongSpecificName"]];
specificNameRange.location++;
specificNameRange.length -= 2;
[attrString setAttributes:specificNameAttributes range:specificNameRange];
[btnWelcome setAttributedTitle:attrString forState:UIControlStateNormal];
The btnWelcome width is set to width of UIViewController.view
The Color Attribute of specificName is very well done.
But, setLineBreakMode:NSLineBreakByTruncatingTail doesn't apply.
Just shows below.
Welcome To 'VeryVeryLongSpec
The contents is just clipped.
The expectation result is below.
Welcome To 'VeryVeryLong...' World!
I am trying to create a UILabel where some of the text is aligned to the right and some of the text is aligned to the left. It is similar to the UITableViewCell with the small arrow:
I am trying to do it with NSAttributedString , but can't figure out what is the correct way to tackle this.
Here is some code which isn't working. It is aligned to the right.
NSMutableAttributedString *att = [[NSMutableAttributedString alloc] initWithString:#"Label >"];
NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init];
paragraph.alignment = NSTextAlignmentLeft;
[att addAttribute:NSParagraphStyleAttributeName value:paragraph range:NSMakeRange(0, #"Label".length)];
NSMutableParagraphStyle *rightParagraph = [[NSMutableParagraphStyle alloc] init];
paragraph.alignment = NSTextAlignmentRight;
[att addAttribute:NSParagraphStyleAttributeName value:rightParagraph range:NSMakeRange(5, 1)];
You can use NSAttributedString to achieve your requirements, but it will be much better and cleaner approach to use two UILabels instead.
Use 2 labels.Assign the needed TextAlignment property to them. And after setting label text value, write this line :
[textLabel sizeToFit];
Though sizes of the labels varies it will set to minimum size. and will avoid text overlap.
I did it before with that code, Hope it also working for you.
NSString* alphaString = #“some text”;
NSMutableParagraphStyle* paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.alignment = NSTextAlignmentLeft;
NSMutableAttributedString * attributedString = [[NSMutableAttributedString alloc]
initWithString:alphaString
attributes:[NSDictionary dictionaryWithObjectsAndKeys:
[UIFont fontWithName:#"HelveticaNeue" size:13], NSFontAttributeName,
paragraphStyle, NSParagraphStyleAttributeName, nil]];
NSString * betaString = #“some other text”;
NSMutableParagraphStyle* paragraphStyle2 = [[NSMutableParagraphStyle alloc] init];
paragraphStyle2.alignment = NSTextAlignmentRight;
[attributedString appendAttributedString:[[NSAttributedString alloc] initWithString:betaString attributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont fontWithName:#"HelveticaNeue" size:13], NSFontAttributeName, paragraphStyle2, NSParagraphStyleAttributeName, nil]]];
yourLabel.attributedText = attributedString;
I have a UITextView and there are certain words I'm casting with NSString stringWithFormat that I'd like to be bolded.
I have looked around Stack Overflow and tried to follow the the postings but I guess I'm not understanding it.
Here's what I've been playing around with:
NSRange boldedRange = NSMakeRange(0, 4);
NSString *boldFontName = [[UIFont fontWithName:#"Helvetica-Bold" size:100]fontName];
NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:self.name];
[attrString beginEditing];
[attrString addAttribute:NSFontAttributeName
value:boldFontName
range:boldedRange];
[attrString endEditing];
self.resultsTextView.attributedText = attrString;
self.resultsTextView.text = [NSString stringWithFormat:#"One day, %# was taking a walk and saw a %# boy. He was %# a %#.", attrString, self.adjective, self.adverb, self.noun];
You can also set it the following way if you want by setting a dictionary as a whole, as attribute
NSString *strTextView = #"This is some demo Text to set BOLD";
NSRange rangeBold = [strTextView rangeOfString:#"BOLD"];
UIFont *fontText = [UIFont boldSystemFontOfSize:10];
NSDictionary *dictBoldText = [NSDictionary dictionaryWithObjectsAndKeys:fontText, NSFontAttributeName, nil];
NSMutableAttributedString *mutAttrTextViewString = [[NSMutableAttributedString alloc] initWithString:strTextView];
[mutAttrTextViewString setAttributes:dictBoldText range:rangeBold];
[textViewTermsPolicy setAttributedText:mutAttrTextViewString];
Use the below code to set Attribute string in TextView.
NSString *infoString =#"I am Kirit Modi from Deesa.";
NSMutableAttributedString *attString=[[NSMutableAttributedString alloc] initWithString:infoString];
UIFont *font_regular=[UIFont fontWithName:#"Helvetica" size:20.0f];
UIFont *font_bold=[UIFont fontWithName:#"Helvetica-Bold" size:20.0f];
[attString addAttribute:NSFontAttributeName value:font_regular range:NSMakeRange(0, 4)];
[attString addAttribute:NSFontAttributeName value:font_bold range:NSMakeRange(5, 15)];
[attString addAttribute:NSFontAttributeName value:font_regular range:NSMakeRange(16, infoString.length - 15 - 1)];
[self.txtView setAttributedText:attString];
OutPut :
Check out #CrazyYoghurt improvement on #Bbrame and #BenoitJadinon on this previous SO question 3586871
I've been using it for over a year and it works great. One limitation: I don't think you can bold multiple times the same string if it appears more than once in your original string. But you can probably expend the code to make it do so if you wish.
If you also need to filter some word from the UITextView and make it underline/change color of that particular text only then you can use the below code.
Here, I'm getting all the doc text in the Content string and filter some particular text that is in Hebrew language.
NSMutableAttributedString *aStr = [[NSMutableAttributedString alloc]initWithString:content attributes:nil];
[aStr addAttribute:NSLinkAttributeName value:#"http://www.apple.com" range:[content rangeOfString:#"מדיניות פרטיות"]];
[aStr addAttribute:NSLinkAttributeName value:#"http://www.google.com" range:[content rangeOfString:#"לינק"]];
textview.linkTextAttributes = #{NSUnderlineStyleAttributeName : #(NSUnderlineStyleSingle)};
textview.delegate = (id)self;
//...You can as per your custom color
[aStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:[content rangeOfString:#"מדיניות פרטיות"]];
[aStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:[content rangeOfString:#"לינק"]];
//Here You can also add the tap gesture on that text.
//Tap gesture
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tappedTextView:)];
[textview addGestureRecognizer:tapRecognizer];
[textview setAttributedText:aStr];
textview.textAlignment=NSTextAlignmentRight;
//For getting the text location in the tap gesture
-(void)tappedTextView:(UITapGestureRecognizer *)tapGesture
{
UITextView *textView = (UITextView *)tapGesture.view;
CGPoint tapLocation = [tapGesture locationInView:textView];
UITextPosition *textPosition = [textView closestPositionToPoint:tapLocation];
NSDictionary *attributes = [textView textStylingAtPosition:textPosition inDirection:UITextStorageDirectionForward];
NSString *urlStr = attributes[NSLinkAttributeName];
if (urlStr)
{
//[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:#"%#",url]]];
PrivacyViewController *next = [PrivacyViewController new];
[self.navigationController pushViewController:next animated:YES];
}
}
Is there any support for spannable string in iOS?
I would like to create an underlineSpan and on click of it and then open some other view controller.
NSAttributedString *title;
title = [[NSAttributedString alloc] initWithString:#"hello how r u..." attributes:#{ NSFontAttributeName : [UIFont fontWithName:#"Noteworthy-Bold" size:15], NSUnderlineStyleAttributeName : #1 , NSStrokeColorAttributeName : [UIColor blackColor]}]; //1
UILabel *label;
label = [[UILabel alloc] initWithFrame:CGRectMake( (self.view.bounds.size.width - title.size.width) / 2.0f, 40.0f, title.size.width, title.size.height)]; //2
label.attributedText = title; //3
[self.view addSubview:label];
Yes, they are called NSAttributedStrings in iOS.
Example Code to Add underline
NSString *str = #"Amit";
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:str];
[attributedString addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInt:NSUnderlineStyleSingle] range:NSMakeRange(0, 4)];
More info # Apple Documentation
To add link to that underline you check out this code:
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:#"Google Webpage"];
[attributedString addAttribute:NSLinkAttributeName
value:#"http://www.google.com"
range:[[attributedString string] rangeOfString:#"Google Webpage"]];
NSDictionary *linkAttributes = #{NSForegroundColorAttributeName: [UIColor greenColor],
NSUnderlineColorAttributeName: [UIColor lightGrayColor],
NSUnderlineStyleAttributeName: #(NSUnderlinePatternSolid)};
// assume that textView is a UITextView previously created (either by code or Interface Builder)
textView.linkTextAttributes = linkAttributes; // customizes the appearance of links
textView.attributedText = attributedString;
textView.delegate = self;
This source code was found # Raywenderlich website
I have builed a button with two titles line by this code:
rootBntUI.titleLabel.font = [UIFont fontWithName:#"Avenir-Black" size:UserListFontSize];
[rootBntUI.layer setBorderWidth:0];
rootBntUI.titleLabel.textColor = [UIColor whiteColor];
rootBntUI.titleLabel.lineBreakMode = NSLineBreakByTruncatingTail;
rootBntUI.titleLabel.textAlignment = NSTextAlignmentCenter;
rootBntUI.titleLabel.numberOfLines = 2;
Everything is working fine but how can I control line spacing of button title?
You can do the styling from the xib . Use button title attributed in attribute inspector and you can set all the styling parameter along with spacing .
I have resolved my problem, and this solution for anyone who have similar question.
NSMutableParagraphStyle *style = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
[style setAlignment:NSTextAlignmentCenter];
[style setLineBreakMode:NSLineBreakByWordWrapping];
[style setLineSpacing:-50];
UIFont *font1 = [UIFont fontWithName:#"Avenir-Black" size:UserListFontSize];
NSDictionary *dict1 = #{NSUnderlineStyleAttributeName:#(NSUnderlineStyleSingle),
NSFontAttributeName:font1,
NSParagraphStyleAttributeName:style};
NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] init];
[attString appendAttributedString:[[NSAttributedString alloc] initWithString:[NSString stringWithFormat:#"%#", obj] attributes:dict1]];
[FriendBnt setAttributedTitle:attString forState:UIControlStateNormal];
[[FriendBnt titleLabel] setNumberOfLines:0];
[[FriendBnt titleLabel] setLineBreakMode:NSLineBreakByWordWrapping];
Happy coding.
This works in Swift 2 using .lineHeightMultiple to compress the title text on a button.
let style = NSMutableParagraphStyle()
style.lineHeightMultiple = 0.8
style.alignment = .Center
style.lineBreakMode = .ByWordWrapping
let dict1:[String:AnyObject] = [
NSParagraphStyleAttributeName: style,
NSUnderlineStyleAttributeName: NSUnderlineStyle.StyleSingle.rawValue
]
let attrString = NSMutableAttributedString()
attrString.appendAttributedString(NSAttributedString(string: "Button Text here over two lines", attributes: dict1))
myButton.setAttributedTitle(attrString, forState: .Normal)
myButton.titleLabel?.numberOfLines = 0
What really worked for me to change line height of the UIButton title label, was this:
NSMutableParagraphStyle *style = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
style.maximumLineHeight = 12.0;
style.minimumLineHeight = 12.0;
UIColor *colorO = [UIColor whiteColor];
UIColor *colorD = [UIColor redColor];
NSDictionary *firstAttributes = #{NSFontAttributeName : [UIFont fontWithName:#"HelveticaNeue-CondensedBold" size:getFloatScaledFactor(13.0)],
NSForegroundColorAttributeName : colorO,
NSParagraphStyleAttributeName:style
};
NSDictionary *secondAttributes = #{NSFontAttributeName : [UIFont fontWithName:#"HelveticaNeue-CondensedBold" size:getFloatScaledFactor(13.0)],
NSForegroundColorAttributeName : colorD,
NSParagraphStyleAttributeName:style
};
NSArray *textArray = [title componentsSeparatedByString:#"\n"];
NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] init];
[attString appendAttributedString:[[NSAttributedString alloc] initWithString:[NSString stringWithFormat:#"%#", textArray[0]] attributes:firstAttributes]];
[attString appendAttributedString:[[NSAttributedString alloc] initWithString:[NSString stringWithFormat:#"%#", textArray[1]] attributes:secondAttributes]];
[self.btnRight setAttributedTitle:attString forState:UIControlStateNormal];
As a alternative solution.