質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
iOS

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

Q&A

解決済

1回答

1799閲覧

CTCheckboxの大きさを変更したいのですが

pinokio

総合スコア32

iOS

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

0グッド

0クリップ

投稿2015/11/28 10:25

CTCheckboxというチェックボックスを作れるFrameworkがあります。
ライブラリの中身は以下のようになっています。
私はチェックボックスのサイズを大きくしたいので、CTCheckboxDefaultSideLength を40から100に変更したのですが、大きさが変りませんでした。
どうしたら、大きさを変えられるのでしょうか?

#import "CTCheckbox.h" static const float CTCheckboxDefaultSideLength = 100.0; @interface CTCheckbox () @property (nonatomic, strong) NSMutableDictionary *colorDictionary; @property (nonatomic, strong) NSMutableDictionary *backgroundColorDictionary; @end @implementation CTCheckbox - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { [self setUp]; } return self; } - (id)initWithCoder:(NSCoder *)aDecoder { self = [super initWithCoder:aDecoder]; if (self) { [self setUp]; } return self; } - (void)setUp { self.colorDictionary = [NSMutableDictionary dictionary]; self.backgroundColorDictionary = [NSMutableDictionary dictionary]; self.checkboxSideLength = CTCheckboxDefaultSideLength; self.checkboxColor = [UIColor blackColor]; self.backgroundColor = [UIColor clearColor]; self.textLabel = [[UILabel alloc] initWithFrame:CGRectZero]; self.textLabel.backgroundColor = [UIColor clearColor]; [self addSubview:self.textLabel]; [self addObserver:self forKeyPath:@"enabled" options:NSKeyValueObservingOptionNew context:nil]; [self addObserver:self forKeyPath:@"selected" options:NSKeyValueObservingOptionNew context:nil]; [self addObserver:self forKeyPath:@"highlighted" options:NSKeyValueObservingOptionNew context:nil]; } - (void)dealloc { [self removeObserver:self forKeyPath:@"enabled"]; [self removeObserver:self forKeyPath:@"selected"]; [self removeObserver:self forKeyPath:@"highlighted"]; } - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { if ([keyPath isEqualToString:@"enabled"] || [keyPath isEqualToString:@"selected"] || [keyPath isEqualToString:@"highlighted"]) { [self changeColorForState:self.state]; [self changeBackgroundColorForState:self.state]; } else { [super observeValueForKeyPath:keyPath ofObject:object change:change context:context]; } } - (void)changeColorForState:(UIControlState)state { UIColor *color; switch (state) { case UIControlStateNormal: color = self.colorDictionary[@(UIControlStateNormal)]; break; case UIControlStateSelected: color = self.colorDictionary[@(UIControlStateSelected)]; break; case UIControlStateDisabled: color = self.colorDictionary[@(UIControlStateDisabled)]; break; default: break; } if (!color) { color = [UIColor blackColor]; } self.checkboxColor = color; self.textLabel.textColor = color; } - (void)changeBackgroundColorForState:(UIControlState)state { UIColor *color; switch (state) { case UIControlStateNormal: color = self.backgroundColorDictionary[@(UIControlStateNormal)]; break; case UIControlStateSelected: color = self.backgroundColorDictionary[@(UIControlStateSelected)]; break; case UIControlStateDisabled: color = self.backgroundColorDictionary[@(UIControlStateDisabled)]; break; default: break; } if (!color) { color = [UIColor clearColor]; } self.backgroundColor = color; } - (void)setChecked:(BOOL)checked { [self setChecked:checked withEvent:YES]; } -(void)setChecked:(BOOL)checked withEvent:(BOOL)withEvent { _checked = checked; [self setNeedsDisplay]; if(withEvent) [self sendActionsForControlEvents:UIControlEventValueChanged]; } - (void)setCheckboxColor:(UIColor *)checkboxColor { _checkboxColor = checkboxColor; [self setNeedsDisplay]; } - (void)setColor:(UIColor *)color forControlState:(UIControlState)state { switch (state) { case UIControlStateNormal: self.colorDictionary[@(UIControlStateNormal)] = color; break; case UIControlStateSelected: self.colorDictionary[@(UIControlStateSelected)] = color; break; case UIControlStateDisabled: self.colorDictionary[@(UIControlStateDisabled)] = color; break; default: break; } [self changeColorForState:self.state]; } - (void)setBackgroundColor:(UIColor *)backgroundColor forControlState:(UIControlState)state { switch (state) { case UIControlStateNormal: self.backgroundColorDictionary[@(UIControlStateNormal)] = backgroundColor; break; case UIControlStateSelected: self.backgroundColorDictionary[@(UIControlStateSelected)] = backgroundColor; break; case UIControlStateDisabled: self.backgroundColorDictionary[@(UIControlStateDisabled)] = backgroundColor; break; default: break; } [self changeBackgroundColorForState:self.state]; } - (void)drawRect:(CGRect)rect { CGRect frame = CGRectIntegral(CGRectMake(0, (rect.size.height - self.checkboxSideLength) / 2.0, self.checkboxSideLength, self.checkboxSideLength)); if (self.checked) { UIBezierPath *bezierPath = [UIBezierPath bezierPath]; [bezierPath moveToPoint:CGPointMake(CGRectGetMinX(frame) + 0.75000 * CGRectGetWidth(frame), CGRectGetMinY(frame) + 0.21875 * CGRectGetHeight(frame))]; [bezierPath addLineToPoint:CGPointMake(CGRectGetMinX(frame) + 0.40000 * CGRectGetWidth(frame), CGRectGetMinY(frame) + 0.52500 * CGRectGetHeight(frame))]; [bezierPath addLineToPoint:CGPointMake(CGRectGetMinX(frame) + 0.28125 * CGRectGetWidth(frame), CGRectGetMinY(frame) + 0.37500 * CGRectGetHeight(frame))]; [bezierPath addLineToPoint:CGPointMake(CGRectGetMinX(frame) + 0.17500 * CGRectGetWidth(frame), CGRectGetMinY(frame) + 0.47500 * CGRectGetHeight(frame))]; [bezierPath addLineToPoint:CGPointMake(CGRectGetMinX(frame) + 0.40000 * CGRectGetWidth(frame), CGRectGetMinY(frame) + 0.75000 * CGRectGetHeight(frame))]; [bezierPath addLineToPoint:CGPointMake(CGRectGetMinX(frame) + 0.81250 * CGRectGetWidth(frame), CGRectGetMinY(frame) + 0.28125 * CGRectGetHeight(frame))]; [bezierPath addLineToPoint:CGPointMake(CGRectGetMinX(frame) + 0.75000 * CGRectGetWidth(frame), CGRectGetMinY(frame) + 0.21875 * CGRectGetHeight(frame))]; [bezierPath closePath]; [self.checkboxColor setFill]; [bezierPath fill]; } UIBezierPath *roundedRectanglePath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(CGRectGetMinX(frame) + floor(CGRectGetWidth(frame) * 0.05000 + 0.5), CGRectGetMinY(frame) + floor(CGRectGetHeight(frame) * 0.05000 + 0.5), floor(CGRectGetWidth(frame) * 0.95000 + 0.5) - floor(CGRectGetWidth(frame) * 0.05000 + 0.5), floor(CGRectGetHeight(frame) * 0.95000 + 0.5) - floor(CGRectGetHeight(frame) * 0.05000 + 0.5)) cornerRadius: 50]; roundedRectanglePath.lineWidth = 2 * self.checkboxSideLength / CTCheckboxDefaultSideLength; [self.checkboxColor setStroke]; [roundedRectanglePath stroke]; } - (void)layoutSubviews { [super layoutSubviews]; CGFloat textLabelOriginX = self.checkboxSideLength + 5.0; CGSize textLabelMaxSize = CGSizeMake(CGRectGetWidth(self.bounds) - textLabelOriginX, CGRectGetHeight(self.bounds)); CGRect r = [self.textLabel.text boundingRectWithSize:textLabelMaxSize options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading) attributes:@{NSFontAttributeName:self.textLabel.font} context:nil]; CGSize textLabelSize = CGSizeMake(r.size.width, r.size.height); self.textLabel.frame = CGRectIntegral(CGRectMake(textLabelOriginX, (CGRectGetHeight(self.bounds) - textLabelSize.height) / 2.0, textLabelSize.width, textLabelSize.height)); } - (void)endTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event { CGPoint location = [touch locationInView:self]; if (CGRectContainsPoint(self.bounds, location)) { self.checked = !self.checked; } } @end

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

CTCheckbox Github

こちらからDownloadしたサンプルを参考に、Swift用に変更して(Swiftタグが付いていたので....)出来ましたので変更点をのせておきます。

objectivec

1 2// ライブラリ内で変更したのは以下のみ 3static const float CTCheckboxDefaultSideLength = 100.0;

Bridging-Header.hに以下を追加
#import "CTCheckbox.h"

swift

1 2 3class ViewController: UIViewController { 4 5 @IBOutlet weak var checkbox: CTCheckbox! 6 var checkbox2: CTCheckbox! 7 8 override func viewDidLoad() { 9 super.viewDidLoad() 10 11 checkbox.addTarget(self, action: "checkboxDidChange:", forControlEvents: .ValueChanged) 12 checkbox.textLabel.text = "100 × 100" 13 checkbox.setColor(UIColor.blackColor(), forControlState: .Normal) 14 checkbox.setColor(UIColor.grayColor(), forControlState: .Disabled) 15 16 17 checkbox2 = CTCheckbox(frame: CGRect(x: 40, y: 160, width: 100, height: 100)) 18 checkbox2.setColor(UIColor.blueColor(), forControlState: .Normal) 19 self.view.addSubview(checkbox2) 20 } 21 22 func checkboxDidChange(checkBox: CTCheckbox) { 23 print("Checked!") 24 } 25} 26

StoryBoardのUIViewのサイズを100×100にする
StoryBoardのUIViewのサイズを100×100にする

以下の用に表示されました
以下の用に表示されました

投稿2015/11/28 13:04

_Kentarou

総合スコア8490

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

pinokio

2015/11/29 12:17

回答ありがとうございました! 大きさを変えることができました!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問