将UISlider连接到fontSize

时间:2012-12-24 23:35:54

标签: iphone ios

将UISlider连接到fontSize [(IBAction)font1和(IBAction)font2]的可能方法。这是代码

·H

@interface ViewController : UIViewController
{
    IBOutlet UISlider* fontSizeSlider;
    IBOutlet UITextView* textView;
}
-(IBAction)sliderValueChanged:(UISlider*)sender;
-(IBAction)font1;
-(IBAction)font2;
@end

的.m

-(IBAction)sliderValueChanged:(UISlider*)sender
{
   textView.font = [UIFont fontWithName:@"??????" size:sender.value];
}

-(IBAction)font1{
   textView.font = [UIFont fontWithName:@"Heiti TC" size:18];
}

-(IBAction)font2{
    textView.font = [UIFont fontWithName:@"Marker Felt" size:18];
}

-(IBAction)font1; //[UIButton]

-(IBAction)font1; //[UIButton]

-(IBAction)sliderValueChanged; //[UISlider]

3 个答案:

答案 0 :(得分:1)

如果要保留正在使用的字体,只需在移动滑块时调整其大小,就可以抓取现有的字体实例并将其传递给fontWithSize:以获取具有相同面的新字体实例:< / p>

-(IBAction)sliderValueChanged:(UISlider*)sender
{
   textView.font = [textView.font fontWithSize:sender.value];
}

答案 1 :(得分:1)

如果我理解正确,您希望能够从两种字体中进行选择。因此,您必须将字体名称存储在NSString中。像这样:

·H

@interface ViewController : UIViewController
{
    IBOutlet UISlider* fontSizeSlider;
    IBOutlet UITextView* textView;
    NSString *currentFont;
}
-(IBAction)sliderValueChanged:(UISlider*)sender;
-(IBAction)font1;
-(IBAction)font2;
@end

的.m

-(void)viewDidLoad {
    [super viewDidLoad];
    currentFont = @"Heiti TC"; //Or whatever your default font is
    textView.font = [UIFont fontWithName:currentFont size:sender.value];
}
-(IBAction)sliderValueChanged:(UISlider*)sender
{
   textView.font = [UIFont fontWithName:currentFont size:sender.value];
}

-(IBAction)font1{
   currentFont = @"Heiti TC";
   textView.font = [UIFont fontWithName:currentFont size:fontSizeSlider.value];
}

-(IBAction)font2{
   currentFont = @"Marker Felt";
   textView.font = [UIFont fontWithName:currentFont size:fontSizeSlider.value];
}

答案 2 :(得分:0)

如果我正确理解你,你想根据滑块的值改变字体大小。尝试使用此代码根据滑块值更改字体大小:

- (IBAction)sliderChangeFontSize:(id)sender {
     textView.font = [UIFont fontWithName:@"Heiti TC" size:slider.value];
}

然后将IBAction挂钩到nib文件中的Slider,并选择Sent Event为“Value Changed”

使用您正在使用的方法应该可行。唯一的问题是您必须将滑块委托连接到View Controller,以便您能够使用发送方值。