[Swift]名前付タプル配列
名前付タプル配列
var draws:[(x:CGFloat, y:CGFloat, width:CGFloat, height:CGFloat)] = [] self.draws.append( (x:0.0, y:0.0, width:100.0, height:0.0) )
名前付タプル配列
var draws:[(x:CGFloat, y:CGFloat, width:CGFloat, height:CGFloat)] = [] self.draws.append( (x:0.0, y:0.0, width:100.0, height:0.0) )
クラス拡張 objective-cにおけるカテゴリ
extension UIView { var left:CGFloat{ get{ return self.frame.origin.x } set{ self.frame = CGRectMake(newValue, self.frame.origin.y, self.frame.size.width, self.frame.size.height) } } func toUIImage() -> UIImage{ UIGraphicsBeginImageContextWithOptions(self.bounds.size, false, 0.0) self.layer.renderInContext(UIGraphicsGetCurrentContext()) let image:UIImage = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() return image } }
String整形
String(format: "%04d-%02d-%02d %02d:%02d:%02d",year,month,day,hour,min,sec)
NSTimerがFireしない
UIAlertViewのYes,Noボタンをタップしたタイミングと重なったり、
何らかの要因によりNSTimerがサブスレッドで実行されることが原因
対処法
[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(methodName) userInfo:nil repeats:NO];
これを
dispatch_async(dispatch_get_main_queue(), ^{ [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(methodName) userInfo:nil repeats:NO]; });
こうする
日付文字列まとめ
_currentDate = [NSDate dateWithTimeIntervalSinceNow:[[NSTimeZone systemTimeZone] secondsFromGMT]]; NSDateFormatter* form = [[NSDateFormatter alloc] init]; [form setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]]; [form setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; _currentDatetimeString = [form stringFromDate: _currentDate ]; [form setDateFormat:@"yyyy-MM-dd"]; _currentDateString = [form stringFromDate: _currentDate ]; [form setDateFormat:@"yyyy-MM"]; _currentYMString = [form stringFromDate: _currentDate ]; [form setDateFormat:@"HH:mm:ss"]; _currentTimeString = [form stringFromDate: _currentDate ];
RecurrenceなEKEventを全て削除
EKEventStore*eventstore = [[EKEventStore alloc] init]; EKEvent*firstEvent = [eventstore eventWithIdentifier:targetRecurrenceEvent.eventIdentifier]; NSError*error; BOOL result = [eventstore removeEvent:firstEvent span:EKSpanFutureEvents commit:YES error:&error];
UILabelにマージンを設ける
サブクラスを作って下記を記述
- (void)drawTextInRect:(CGRect)rect { UIEdgeInsets insets = {0, 2, 0, 0}; [super drawTextInRect:UIEdgeInsetsInsetRect(rect, insets)]; }
UIScrollViewのサブクラスを作って下記を記述
-(BOOL)touchesShouldCancelInContentView:(UIView *)view{ return YES; }
UILabelのテキストを省略しない
label.lineBreakMode = NSLineBreakByClipping;
UIWebViewのhtmlにUIImageをimgタグで表示
NSData *imageData = UIImagePNGRepresentation(image); NSString* base64 = [imageData base64EncodedStringWithOptions:NSDataBase64Encoding76CharacterLineLength]; base64 = [base64 stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; NSString*js = [NSString stringWithFormat:@"var img=document.createElement('img');img.src='data:image/jpg;base64,%@';document.body.appendChild(img);",base64]; [mainWebView stringByEvaluatingJavaScriptFromString:js];