2018年1月9日 星期二

在段落中某些特定文字加入連結

  • 只有UITextView可以做到。
  • 要實作UITextViewDelegate
self.linkTextView.delegate = self; 

#pragma mark - UITextViewDelegate
- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(nonnull NSURL *)URL inRange:(NSRange)characterRange {
     //可以做些判斷
    return YES;
}

  • 加入某段文字 
NSString *str = @"For help please visit our Knowledge base";
    NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:str];
    [attrStr addAttribute:NSLinkAttributeName value:[NSURL URLWithString:@"https://yourUrl.com"] range:[str rangeOfString:@"Knowledge base"]];
    self.linkTextView.attributedText = attrStr;
  
  • 整段文字
NSDictionary *dictAttr = @{NSLinkAttributeName:[NSURL URLWithString:@"http://yourUrl.com"]};
    NSAttributedString *attrStr = [[NSAttributedString alloc]initWithString:@"please visit our Knowledge base" attributes:dictAttr];
    self.linkTextView.attributedText = attrStr;

2018年1月3日 星期三

去特定的setting頁

// wifi settings 
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"App-Prefs:root=WIFI"]];
 
// Bluetooth settings 
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"App-Prefs:root=Bluetooth"]]; 

2017年12月12日 星期二

取得webview的內容size

https://stackoverflow.com/questions/3936041/how-to-determine-the-content-size-of-a-uiwebview
 
- (void)webViewDidFinishLoad:(UIWebView *)aWebView {
    CGRect frame = aWebView.frame;
    frame.size.height = 1;
    aWebView.frame = frame;
    CGSize fittingSize = [aWebView sizeThatFits:CGSizeZero];
    frame.size = fittingSize;
    aWebView.frame = frame;

    NSLog(@"size: %f, %f", fittingSize.width, fittingSize.height);
}
 
要先設height到很小,再做[webView sizeToFit]; 或 [webView sizeThatFits:CGSizeZero];

2017年12月5日 星期二

正方形圖片

+ (UIImage *)resizeToSquareImageWithImage:(UIImage*)image toSize:(CGSize)newSize
{
    // crop image to square one
    CGSize oldSize = image.size;
    double newWidth = oldSize.width > oldSize.height ? oldSize.height : oldSize.width;
    CGRect newRect = CGRectMake(0.0, 0.0, newWidth, newWidth);

    CGImageRef sourceImageRef = [image CGImage];
    CGImageRef newImageRef = CGImageCreateWithImageInRect(sourceImageRef, newRect);
    UIImage *squareImage = [UIImage imageWithCGImage:newImageRef];
    CGImageRelease(newImageRef);
    // resize the image
    UIGraphicsBeginImageContext(newSize);
    [squareImage drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
  
    return newImage;
}

從data查圖片格式

source: https://stackoverflow.com/questions/4147311/finding-image-type-from-nsdata-or-uiimage 
 
+ (NSString *)contentTypeForImageData:(NSData *)data {
    uint8_t c;
    [data getBytes:&c length:1];

    switch (c) {
    case 0xFF:
        return @"image/jpeg";
    case 0x89:
        return @"image/png";
    case 0x47:
        return @"image/gif";
    case 0x49:
    case 0x4D:
        return @"image/tiff";
    }
    return nil;
}

2017年11月30日 星期四