Created
July 8, 2015 03:25
-
-
Save dabing1022/649b8583b48d7808ae55 to your computer and use it in GitHub Desktop.
常用方法片段
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 01. init method forbidden | |
- (id)init { | |
@throw [NSException exceptionWithName:NSInternalInconsistencyException | |
reason:@"-init is not a valid initializer for the class Foo" | |
userInfo:nil]; | |
return nil; | |
} |
// 03. DDLabel
#import <UIKit/UIKit.h>
@interface DDLabel : UILabel
@property (nonatomic) UIEdgeInsets edgeInsets;
- (int)lineCount;
@end
#import "DDLabel.h"
@implementation DDLabel
- (CGRect)textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)numberOfLines
{
UIEdgeInsets insets = self.edgeInsets;
CGRect rect = [super textRectForBounds:UIEdgeInsetsInsetRect(bounds, insets)
limitedToNumberOfLines:numberOfLines];
rect.origin.x -= insets.left;
rect.origin.y -= insets.top;
rect.size.width += (insets.left + insets.right);
rect.size.height += (insets.top + insets.bottom);
self.preferredMaxLayoutWidth = self.bounds.size.width;
return rect;
}
- (void)drawTextInRect:(CGRect)rect
{
[super drawTextInRect:UIEdgeInsetsInsetRect(rect, self.edgeInsets)];
}
- (void)setBounds:(CGRect)bounds {
[super setBounds:bounds];
if (self.numberOfLines == 0 && bounds.size.width != self.preferredMaxLayoutWidth) {
self.preferredMaxLayoutWidth = self.bounds.size.width;
[self setNeedsUpdateConstraints];
}
}
- (int)lineCount
{
NSString *labelContent = self.text.length == 0 ? self.attributedText.string : self.text;
CGRect rect = [labelContent boundingRectWithSize:CGSizeMake(self.bounds.size.width, MAXFLOAT)
options:NSStringDrawingUsesLineFragmentOrigin
attributes:@{NSFontAttributeName : self.font}
context:nil];
return ceil(rect.size.height / self.font.lineHeight);
}
// 04. Play sound
NSURL *soundurl = [[NSBundle mainBundle] URLForResource: @"mysound" withExtension: @"caf"];
AVAudioPlayer *mySoundPlayer =[[AVAudioPlayer alloc] initWithContentsOfURL:soundurl error:&error];
mySoundPlayer .volume=0.4f; //between 0 and 1
[mySoundPlayer prepareToPlay];
mySoundPlayer.numberOfLoops=0; //or more if needed
[mySoundPlayer play];
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
// 02. mask view with rounding corners