Skip to content

Instantly share code, notes, and snippets.

@dabing1022
Created July 8, 2015 03:25
Show Gist options
  • Save dabing1022/649b8583b48d7808ae55 to your computer and use it in GitHub Desktop.
Save dabing1022/649b8583b48d7808ae55 to your computer and use it in GitHub Desktop.
常用方法片段
// 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;
}
@dabing1022
Copy link
Author

// 02. mask view with rounding corners

-(void) setMaskTo:(UIView*)view byRoundingCorners:(UIRectCorner)corners
{
    UIBezierPath* rounded = [UIBezierPath bezierPathWithRoundedRect:view.bounds byRoundingCorners:corners cornerRadii:CGSizeMake(10.0, 10.0)];

    CAShapeLayer* shape = [[CAShapeLayer alloc] init];
    [shape setPath:rounded.CGPath];

    view.layer.mask = shape;
}

[self setMaskTo:self.photoView byRoundingCorners:UIRectCornerTopLeft|UIRectCornerBottomLeft];

@dabing1022
Copy link
Author

// 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);
}

@dabing1022
Copy link
Author

// 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