I try to display a checkmark symbol like this:
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.alignment = NSTextAlignmentCenter;
paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
[#"✓" drawInRect: CGRectInset(ovalRect, 0/116.0*width, 11/116.0*width) withAttributes:[NSDictionary dictionaryWithObjects:#[[UIFont fontWithName: #"HelveticaNeue" size: 80.0/116.0*width], paragraphStyle, [UIColor orangeColor]] forKeys:#[NSFontAttributeName, NSParagraphStyleAttributeName, NSForegroundColorAttributeName]]];
On http://iosfonts.com the symbol looks just like I want it for Helvetica Neue. However, on the device/ simulator it looks completely different. I found about 3 fonts where it looked different but for the other 20 fonts I tested it always looked the same, always different from http://iosfonts.com. How can I find out how the checkmark symbol would look in different iOS fonts? I don't want to try them all out by hand.
If someone else has font problems, here is the code to display text in all available fonts in an UITableView.
- (void)viewDidLoad
{
[super viewDidLoad];
//Fill NSMutableArray fonts with all available fonts
fonts = [NSMutableArray array];
NSArray *familyNames = [[NSArray alloc] initWithArray:[UIFont familyNames]];
NSArray *fontNames;
NSInteger indFamily, indFont;
for (indFamily = 0; indFamily < [familyNames count]; ++indFamily)
{
fontNames = [[NSArray alloc] initWithArray:[UIFont fontNamesForFamilyName:[familyNames objectAtIndex:indFamily]]];
for (indFont = 0; indFont < [fontNames count]; ++indFont)
{
[fonts addObject:[fontNames objectAtIndex:indFont]];
}
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:indexPath];
NSString *font = [fonts objectAtIndex:indexPath.row];
[cell.textLabel setText:[NSString stringWithFormat:#"✓ %#", font]];
[cell.textLabel setFont:[UIFont fontWithName:font size:16]];
return cell;
}
Related
I want to merge NSString and NSMutableAttributedString .
In below code i want to make self.txtSearch as custom bold size and color.
code -
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
SearchViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"SearchViewCell"];
AutoSuggestModel *autoSuggestModel = [self.autoSuggestArray objectAtIndex:indexPath.row];
if ([[autoSuggestModel type] isEqualToString:#"product"] || [[autoSuggestModel type] isEqualToString:#"category"]){
cell.lblText.text = [NSString stringWithFormat:#"%# in %#", self.txtSearch , [autoSuggestModel label]] ;
}else{
cell.lblText.text = [autoSuggestModel label];
}
return cell;
}
I could make bold particular string with below code. but i want to append both string.
NSMutableAttributedString *boldString = [[NSMutableAttributedString alloc] initWithString:self.txtSearch];
NSRange boldRange = [[autoSuggestModel label] rangeOfString:self.txtSearch];
[boldString addAttribute: NSFontAttributeName value:[UIFont boldSystemFontOfSize:16] range:boldRange];
[cell.lblText setAttributedText: boldString];
update you method like bellow
if ([[autoSuggestModel type] isEqualToString:#"product"] || [[autoSuggestModel type] isEqualToString:#"category"]){
cell.textLabel.attributedText = [self getBoldText:cell withSearch:#"search text" withAutoSuggestModel:#"autoSuggestModel"];
}else{
cell.lblText.text = [autoSuggestModel label];
}
//check above code by the following method
-(NSMutableAttributedString*)getBoldText:(UITableViewCell*)cell withSearch:(NSString*)searchText withAutoSuggestModel:(NSString*)autoSuggestModelText{
NSString *title = [NSString stringWithFormat:#"%# in %#", searchText,autoSuggestModelText];
cell.textLabel.text = title;
UIColor *color = [UIColor redColor];
UIFont *font = [UIFont boldSystemFontOfSize:16];
NSDictionary *attrs = #{NSForegroundColorAttributeName : color,NSFontAttributeName:font};
NSMutableAttributedString * attrStr = [[NSMutableAttributedString alloc] initWithAttributedString:cell.textLabel.attributedText];
[attrStr addAttributes:attrs range:[title rangeOfString:autoSuggestModelText]];
return attrStr;
}
A very cursory look at the NSString documentation has a section called "combining strings" and a method called stringByAppendingString:
Using this method, it should be incredibly straight-forward to accomplish what you're trying to do.
NSMutableAttributedString *boldString = [[NSMutableAttributedString alloc]
initWithString:[self.txtSearch stringByAppendingString:yourMutableString]];
I have a storyboard with tableview. I have 4 cells in table. They have their identifiers. Problem is that I can't change font of cells programmatically. I've already add font to the project and added a new key in the array named "Fonts Provided by application" in the plist file. Maybe I need to reloadData but where I can do it? Under viewWillAppear it's useless
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = [self.menuItems objectAtIndex:indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
[cell.textLabel setFont:[UIFont fontWithName:#"Xiomara" size:30]];
return cell;
}
Make sure that you're using right font name.
You can use this method.
- (void)logFontNamesOfAllFonts
{
NSArray *familyNames = [[NSArray alloc] initWithArray:[UIFont familyNames]];
NSArray *fontNames;
NSInteger indFamily, indFont;
for (indFamily=0; indFamily<[familyNames count]; ++indFamily) {
NSLog(#"Family name: %#", [familyNames objectAtIndex:indFamily]);
fontNames = [[NSArray alloc] initWithArray:
[UIFont fontNamesForFamilyName:
[familyNames objectAtIndex:indFamily]]];
for (indFont=0; indFont<[fontNames count]; ++indFont) {
NSLog(#" Font name: %#", [fontNames objectAtIndex:indFont]);
}
}
}
Just call in your AppDelegate.m's applicationDidFinishLaunching: method, and see in concole the right name of your font.
If there isn't font with name that you installed, then your font not installed correctly.
I am developing iPhone app, in which i got stuck at one point,
what i want to do is, i have UITableView and in the UITableViewCell i want to display the text like this:
India in Country23
Australia in Country2
USA in Country22
AMERI in Country26
FRANCE in Country12
ITALY in Country20
WEST INDIES in Country42
KENYA
SOUTH AFRICA
NEW ZEALAND
The text before in is in one array and text after in (which is in BOLD) is in another array and also i want to change the text color of BOLD text which is after in
Note: Some of the cell contains bold text and some doesn't
How to display data like this in UITableVIew ?
please help and thanks in advance.
Here is a complete example
#implementation MyTableViewController
{
NSArray *_countryNames;
NSArray *_countryNumbers;
}
- (void)viewDidLoad
{
_countryNames = #[#"India", #"Australia", #"USA"];
_countryNumbers = #[#"Country23", #"Country2", #"Country22"];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return MIN(_countryNames.count, _countryNumbers.count);
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
static NSString *identifier = #"reuseIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (!cell)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier: identifier];
}
NSString *text1 = _countryNames[indexPath.row];
NSString *text2 = _countryNumbers[indexPath.row];
NSString *completeText = [[text1 stringByAppendingString:#" " ] stringByAppendingString:text2];
const CGFloat fontSize = 13;
UIFont *boldFont = [UIFont boldSystemFontOfSize:fontSize];
UIFont *regularFont = [UIFont systemFontOfSize:fontSize];
NSDictionary *attrs = #{NSFontAttributeName : regularFont};
NSDictionary *subAttrs = #{NSFontAttributeName : boldFont};
NSRange range = NSMakeRange(text1.length + 1, text2.length);
NSMutableAttributedString *attributedText =
[[NSMutableAttributedString alloc] initWithString:completeText
attributes:attrs];
[attributedText setAttributes:subAttrs range:range];
[cell.textLabel setAttributedText:attributedText];
return cell;
}
#end
I think you need to use custom uilabel with NSAttributedString or an NSMutableAttributedString functionality like this example
NSString *boldFontName = [[UIFont boldSystemFontOfSize:12] fontName];
NSString *yourString = ...;
NSRange boldedRange = NSMakeRange(22, 4);
NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:yourString];
[attrString beginEditing];
[attrString addAttribute:kCTFontAttributeName
value:boldFontName
range:boldedRange];
[attrString endEditing];
[_label setAttributedText:attrString];
I want to be able to change the text in my UITableView cells when they are tapped on (selected/highlighted). The content from each cell is derived from an NSAttributedString. Here is that code:
-(NSAttributedString*)formattedSubject:(int)state {
if(formattedSubject!=nil) return formattedSubject;
NSDictionary *boldStyle = [[NSDictionary alloc] init];
if(state==1) {
boldStyle = #{NSFontAttributeName:[UIFont fontWithName:#"Helvetica-Bold" size:16.0],NSForegroundColorAttributeName:[UIColor colorWithRed:0.067 green:0.129 blue:0.216 alpha:1.0]};
}
else {
boldStyle = #{NSFontAttributeName:[UIFont fontWithName:#"Helvetica-Bold" size:16.0],NSForegroundColorAttributeName:[UIColor whiteColor]};
}
NSDictionary* normalStyle = #{NSFontAttributeName: [UIFont fontWithName:#"Helvetica" size:14.0]};
NSMutableAttributedString* articleAbstract = [[NSMutableAttributedString alloc] initWithString:subject];
[articleAbstract setAttributes:boldStyle range:NSMakeRange(0, subject.length)];
[articleAbstract appendAttributedString:[[NSAttributedString alloc] initWithString:#"\n"]];
int startIndex = [articleAbstract length];
NSTimeInterval _interval=[datestamp doubleValue];
NSDate *date = [NSDate dateWithTimeIntervalSince1970:_interval];
NSDateFormatter *_formatter=[[NSDateFormatter alloc]init];
[_formatter setDateFormat:#"MM/dd/yy"];
NSString* description = [NSString stringWithFormat:#"By %# on %#",author,[_formatter stringFromDate:date]];
[articleAbstract appendAttributedString:[[NSAttributedString alloc] initWithString: description]];
[articleAbstract setAttributes:normalStyle range:NSMakeRange(startIndex, articleAbstract.length - startIndex)];
formattedSubject = articleAbstract;
return formattedSubject;
}
As you can see, I would like the boldStyle text to be a certain color (given by [UIColor colorWithRed:0.067 green:0.129 blue:0.216 alpha:1.0]) when the state is "1", and white otherwise. I've currently tried the following modifications in cellForRowAtIndexPath, as well as didSelectRowAtIndexPath:
cellForRowAtIndexPath:
NSIndexPath *path = [tableView indexPathForSelectedRow];
if(path) {
cell.textLabel.attributedText = [news formattedSubject:1];
}
else {
cell.textLabel.attributedText = [news formattedSubject:0];
}
What I am doing here is checking if a cell is selected, and if so then I changed that cell's "attributedText" to the "0" format. However, this didn't work, and I suspected it was because the modification had to be made when the cell was actually selected. So I tried the following in didSelectRowAtIndexPath:
didSelectRowAtIndexPath:
News *news = newsArray[indexPath.row];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.textLabel.attributedText = [news formattedSubject:0];
However, it seems like the change is never made. I put an NSLog in the "else" part of the boldStyle if/else statement, and it was never written to the console.
Neither of my attempts at a solution have worked, and I've run out of ideas. How can I get the NSAttributedString to change when a cell is highlighted?
Update: It's also interesting that normalStyle text, which is by default black, does turn white when the cell is highlighted/selected when I add cell.textLabel.highlightedTextColor = [UIColor whiteColor]; in cellForRowAtIndexPath, but boldStyle text does not.
I have problem the same, and..
cell.textLabel.enable = NO;
It just resolved
In your cellForRowAtIndexPath method you are checking if a selectedIndexPath in this tableView exists.
NSIndexPath *path = [tableView indexPathForSelectedRow];
if(path) {
I think you want to compare your indexPath and your selectedIndexPath. You should use isEqual: for that.
NSIndexPath *selectedIndexPath = [tableView indexPathForSelectedRow];
if([selectedIndexPath isEqual:indexPath]) {
Guys can someone 'tell me why I can not align well the label in these cell? ... The space between the black and the yellow label should be the same but I can not figure out the exact point you want to change in the code ... Surely it is a trivial but I'm lost
This is the code I'm using
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{
NSString *comment = [[self.objects objectAtIndex:[indexPath row]] objectForKey:#"TITOLO"];
CGFloat whidt = 220;
UIFont *FONT = [UIFont systemFontOfSize:13];
NSAttributedString *attributedText =[[NSAttributedString alloc] initWithString:comment attributes:# { NSFontAttributeName: FONT }];
CGRect rect = [attributedText boundingRectWithSize:(CGSize){whidt, MAXFLOAT}
options:NSStringDrawingUsesLineFragmentOrigin
context:nil];
CGSize size = rect.size;
return size.height +50;
}
- (FFCustomListaEsamiCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object {
FFCustomListaEsamiCell *cell = (FFCustomListaEsamiCell * )[self.tableView dequeueReusableCellWithIdentifier:#"Cell"];
if (!cell) {
cell = [[FFCustomListaEsamiCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"Cell"];
}
NSString *text = [object objectForKey:#"TITOLO"];
CGSize constraint = CGSizeMake(220 , 20000.0f);
UIFont *FONT = [UIFont systemFontOfSize:13];
CGSize size = [text boundingRectWithSize:constraint options:NSStringDrawingUsesLineFragmentOrigin attributes:#{NSFontAttributeName:FONT }context:nil].size;
cell.FFTitoloEsameLabel.frame = CGRectMake(17,10, 240, MAX(size.height, 10.0)+10.0 );
cell.FFTitoloEsameLabel.text = text;
NSDateFormatter *FFDataFormattata = [[NSDateFormatter alloc] init];
[FFDataFormattata setDateFormat:FF_DATE_FORMATTER];
cell.FFDataEsameLabel.text = [NSString stringWithFormat: #"%#",[FFDataFormattata stringFromDate:[object objectForKey:FF_ESAMI_DATA_STRING]]];
PFObject *rowObject = [self.objects objectAtIndex:indexPath.row];
if([[rowObject objectForKey:FF_ESAMI_STATUS] boolValue]) {
cell.last.image = [UIImage imageNamed:#"FFIMG_ClockTAG"];
}
else
{
}
return cell;
}
I solved it by removing the auto-layout and setting the blocks with the size inspector ... I think it's the best solution and that is not a problem especially ... I hope ...