我是编程的新手,我正在尝试创建一个类,它将设置大小,属性,颜色和对齐文本中心,因为我正在使用下面的代码。我想做更多,但一旦我知道如何设置那个类就可以修改。
请帮助。
@implementation OceansTabController
@synthesize textField;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.textField.text = @"\"The sea, once it casts its spell, holds one in its net of wonder forever.\"\nJacques Yves Cousteau\n\nOur majestic oceans\n\n";
NSMutableAttributedString *attString=[[NSMutableAttributedString alloc] initWithString:textField.text];
//
// UIFont *font_regular=[UIFont fontWithName:@"HelveticaNeue" size:14.0f];
UIFont *font=[UIFont fontWithName:@"HelveticaNeue" size:14.0f];
UIFont *font_italic=[UIFont fontWithName:@"HelveticaNeue-Italic" size:14.0f];
UIFont *font_bold=[UIFont fontWithName:@"HelveticaNeue-Bold" size:14.0f];
UIFont *font_boldBlue=[UIFont fontWithName:@"HelveticaNeue-Bold" size:14.0f];
UIFont *font_boldLarge=[UIFont fontWithName:@"HelveticaNeue-Bold" size:16.0f];
UIFont *font_boldLargeCentered=[UIFont fontWithName:@"HelveticaNeue-Bold" size:16.0f];
[attString addAttribute:NSFontAttributeName value:font_italic range:NSMakeRange(0, 75)];
[attString addAttribute:NSFontAttributeName value:font_boldBlue range:NSMakeRange(76, 21)];
[attString addAttribute:NSFontAttributeName value:font_boldLargeCentered range:NSMakeRange(98, 20)];
答案 0 :(得分:0)
你可以创建uitextfield的子类。您可以在其中定义设置属性的方法,如下所示
在customTextField.h
中#import <UIKit/UIKit.h>
@interface customtextField : UITextField
- (void)setCustomTextFieldText:(NSString*)text;
- (void)setCustomTextFieldAttributes;
@end
和customTextField.m
您可以将字体或任何属性定义为mecros
#import "customtextField.h"
#define font [UIFont fontWithName:@"HelveticaNeue" size:14.0f]
#define font_italic [UIFont fontWithName:@"HelveticaNeue-Italic" size:14.0f]
#define font_bold [UIFont fontWithName:@"HelveticaNeue-Bold" size:14.0f]
#define font_boldBlue [UIFont fontWithName:@"HelveticaNeue-Bold" size:14.0f]
#define font_boldLarge [UIFont fontWithName:@"HelveticaNeue-Bold" size:16.0f]
#define font_boldLargeCentered [UIFont fontWithName:@"HelveticaNeue-Bold" size:16.0f]
@implementation customtextField
- (void)setCustomTextFieldText:(NSString*)text{
self.text = text;
}
- (void)setCustomTextFieldAttributes{
NSMutableAttributedString *attString=[[NSMutableAttributedString alloc] initWithString:self.text];
[attString addAttribute:NSFontAttributeName value:font_italic range:NSMakeRange(0, 75)];
[attString addAttribute:NSFontAttributeName value:font_boldBlue range:NSMakeRange(76, 21)];
[attString addAttribute:NSFontAttributeName value:font_boldLargeCentered range:NSMakeRange(98, 20)];}
@end
现在您可以将customTextField用作uitextField,并可以通过在类中导入customTextField.h文件来设置所需的所有属性:
customtextField * txtField = [[customtextField alloc] initWithFrame:CGRectMake(20, 100, 100, 40)];
[txtField setCustomTextFieldText:@"your text for textfield"];
[txtField setCustomTextFieldAttributes];
[self.view addSubview:txtField];
肯定会对你有用..祝你好运
答案 1 :(得分:0)
Shubham bairagi,谢谢你的帮助。再次阅读本文,我确实意识到我并不完全清楚我想要的是什么,因为问题是我不知道所有的条款,要正确地问。无论如何,我找到了解决方案:
NSMutableAttributedString *attString=[[NSMutableAttributedString alloc] initWithString:textField.text];
//
// UIFont *font_regular=[UIFont fontWithName:@"HelveticaNeue" size:14.0f];
UIFont *font=[UIFont fontWithName:@"HelveticaNeue" size:14.0f];
UIFont *font_italic=[UIFont fontWithName:@"HelveticaNeue-Italic" size:14.0f];
UIFont *font_bold=[UIFont fontWithName:@"HelveticaNeue-Bold" size:14.0f];
UIFont *font_boldLarge=[UIFont fontWithName:@"HelveticaNeue-Bold" size:16.0f];
以上内容与我发布的相同,但我添加了这一行来设置我想要的颜色
UIColor *blueColor = [UIColor blueColor];
设置变量(希望我使用正确的术语)
然后我将变量添加到下面的代码中。您会注意到我添加了一个具有相同范围的额外行。这给了我两行具有相同范围的代码(蓝色和粗体)。但是,我发现使用NSForegroundColorAttributeName来代替使用NSFontAttributeName来设置颜色。
[attString addAttribute:NSFontAttributeName value:font_italic range:NSMakeRange(0, 138)];
[attString addAttribute:NSFontAttributeName value:font_bold range:NSMakeRange(139, 21)];
[attString addAttribute:NSForegroundColorAttributeName value:blueColor range:NSMakeRange(139, 21)];
[attString addAttribute:NSFontAttributeName value:font_boldLarge range:NSMakeRange(161, 35)];
如果我不清楚,请告诉我,我会再试一次。
答案 2 :(得分:0)
我只是想出了如何根据上述格式将文本居中
我创建了变量:
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init] ;
[paragraphStyle setAlignment:NSTextAlignmentCenter];
并使用此代码将已添加到SAME范围的文本添加为粗体。
[attString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(161, 35)];